module Admin
  class DashboardStats
    ACTIVE_STATUSES = %w[live reconnecting connecting paused].freeze

    def call
      now = Time.current
      day_start = now.beginning_of_day
      month_start = now.beginning_of_month

      ended_today = StreamSession.where(status: "ended").where("ended_at >= ?", day_start)
      ended_month = StreamSession.where(status: "ended").where("ended_at >= ?", month_start)

      {
        active_sessions: StreamSession.where(status: ACTIVE_STATUSES).count,
        sessions_today: ended_today.count,
        sessions_month: ended_month.count,
        stream_minutes_today: (ended_today.sum(:total_duration_secs).to_i / 60.0).round,
        stream_minutes_month: (ended_month.sum(:total_duration_secs).to_i / 60.0).round,
        teams_count: Team.count,
        users_count: User.count,
        matches_count: Match.count,
        recordings_count: Recording.count,
        recordings_ready: Recording.where(status: "ready").count
      }
    end
  end
end
