# app/admin/ahoy_management.rb
ActiveAdmin.register_page "Ahoy Management" do
  menu label: "访问数据管理", parent: "系统管理"

  content title: "访问数据管理" do
    columns do
      column do
        panel "数据统计" do
          attributes_table_for :ahoy do
            row("总事件数") { Ahoy::Event.count }
            row("总访问数") { Ahoy::Visit.count }
            row("最早事件") { Ahoy::Event.minimum(:time)&.strftime("%Y-%m-%d %H:%M:%S") }
            row("最早访问") { Ahoy::Visit.minimum(:started_at)&.strftime("%Y-%m-%d %H:%M:%S") }
          end
        end
      end

      column do
        panel "操作" do
          div class: "buttons" do
            button_to "立即清理旧数据", admin_ahoy_management_cleanup_path, method: :post,
                      data: { confirm: "确定要清理3个月前的数据吗?" },
                      class: "button"
          end
        end
      end
    end
  end

  page_action :cleanup, method: :post do
    CleanAhoyDataWorker.perform_async
    redirect_to admin_ahoy_management_path, notice: "清理任务已加入队列"
  end
end