# app/admin/sidekiq_tasks.rb
ActiveAdmin.register_page "Sidekiq Tasks" do
  # menu label: "Sidekiq Tasks", priority: 99
  menu label: "Sidekiq Tasks Manager", parent: "系统管理"

  content title: "Sidekiq Tasks Management" do
    div class: "sidekiq-tasks" do
      panel "Manual Task Execution" do
        div class: "task-buttons" do
          div class: "task-button" do
            h3 "Generate Weather Arts"
            form action: admin_sidekiq_tasks_run_task_path, method: :post do
              input type: "hidden", name: "authenticity_token", value: form_authenticity_token
              input type: "hidden", name: "task", value: "GenerateWeatherArtsWorker"
              div class: "input-field" do
                label "City ID"
                input type: "number", name: "city_id", placeholder: "Enter city ID", required: true
              end
              input type: "submit", value: "Run Task", class: "button"
            end
          end

          div class: "task-button" do
            h3 "Batch Generate Weather Arts"
            form action: admin_sidekiq_tasks_run_task_path, method: :post do
              input type: "hidden", name: "authenticity_token", value: form_authenticity_token
              input type: "hidden", name: "task", value: "BatchGenerateWeatherArts"
              input type: "submit", value: "Run Task", class: "button"
            end
          end

          div class: "task-button" do
            h3 "Refresh Sitemap"
            form action: admin_sidekiq_tasks_run_task_path, method: :post do
              input type: "hidden", name: "authenticity_token", value: form_authenticity_token
              input type: "hidden", name: "task", value: "RefreshSitemapWorker"
              input type: "submit", value: "Run Task", class: "button"
            end
          end

          div class: "task-button" do
            h3 "Clean Ahoy Data"
            form action: admin_sidekiq_tasks_run_task_path, method: :post do
              input type: "hidden", name: "authenticity_token", value: form_authenticity_token
              input type: "hidden", name: "task", value: "CleanAhoyDataWorker"
              input type: "submit", value: "Run Task", class: "button"
            end
          end
        end
      end

      panel "Sidekiq Statistics" do
        stats = Sidekiq::Stats.new
        table class: "sidekiq-stats" do
          tr do
            th "Processed Jobs"
            td stats.processed
          end
          tr do
            th "Failed Jobs"
            td stats.failed
          end
          tr do
            th "Enqueued Jobs"
            td stats.enqueued
          end
          tr do
            th "Scheduled Jobs"
            td stats.scheduled_size
          end
          tr do
            th "Retry Set Size"
            td stats.retry_size
          end
        end
      end
    end
  end

  page_action :run_task, method: :post do
    task_name = params[:task]
    city_id = params[:city_id]

    case task_name
    when "BatchGenerateWeatherArts"
      BatchGenerateWeatherArtsWorker.perform_async
    when "GenerateWeatherArtsWorker"
      GenerateWeatherArtWorker.perform_async(city_id)
    when "RefreshSitemapWorker"
      RefreshSitemapWorker.perform_async
    when "CleanAhoyDataWorker"
      CleanAhoyDataWorker.perform_async
    end

    redirect_to admin_sidekiq_tasks_path, notice: "Task #{task_name} has been queued"
  end
end