<?php
declare(strict_types=1);

namespace App\Modules\SportsActivity\Controllers;

use App\Core\Controller;
use App\Core\Request;
use App\Core\Response;
use App\Core\App;

class DashboardController extends Controller
{
    /**
     * Main SportsActivity dashboard with summary cards.
     */
    public function index(Request $request): Response
    {
        $db = App::getInstance()->db();
        $today = date('Y-m-d');

        // Total active disciplines
        $activeDisciplines = (int) ($db->selectOne(
            "SELECT COUNT(*) as cnt FROM sa_disciplines WHERE is_active = 1",
            []
        )['cnt'] ?? 0);

        // Total active facilities + units
        $activeFacilities = (int) ($db->selectOne(
            "SELECT COUNT(*) as cnt FROM sa_facilities WHERE is_active = 1 AND is_archived = 0",
            []
        )['cnt'] ?? 0);

        $activeUnits = (int) ($db->selectOne(
            "SELECT COUNT(*) as cnt FROM sa_facility_units WHERE is_active = 1",
            []
        )['cnt'] ?? 0);

        // Total active coaches
        $activeCoaches = (int) ($db->selectOne(
            "SELECT COUNT(*) as cnt FROM sa_coaches WHERE is_active = 1 AND is_archived = 0",
            []
        )['cnt'] ?? 0);

        // Total registered players (not archived)
        $totalPlayers = (int) ($db->selectOne(
            "SELECT COUNT(*) as cnt FROM sa_players WHERE is_archived = 0",
            []
        )['cnt'] ?? 0);

        // Total active groups + enrolled players
        $activeGroups = (int) ($db->selectOne(
            "SELECT COUNT(*) as cnt FROM sa_groups WHERE status = 'active' AND is_archived = 0",
            []
        )['cnt'] ?? 0);

        $enrolledPlayers = (int) ($db->selectOne(
            "SELECT COUNT(*) as cnt FROM sa_group_players WHERE status = 'active'",
            []
        )['cnt'] ?? 0);

        // Today's bookings count
        $todayBookings = (int) ($db->selectOne(
            "SELECT COUNT(*) as cnt FROM sa_bookings WHERE booking_date = ? AND status NOT IN ('cancelled', 'no_show')",
            [$today]
        )['cnt'] ?? 0);

        // Pending medical approvals
        $pendingMedical = (int) ($db->selectOne(
            "SELECT COUNT(*) as cnt FROM sa_player_documents WHERE document_type = 'medical_cert' AND approval_status = 'pending'",
            []
        )['cnt'] ?? 0);

        // Overdue subscriptions
        $overdueSubscriptions = (int) ($db->selectOne(
            "SELECT COUNT(*) as cnt FROM sa_subscriptions WHERE payment_status IN ('unpaid', 'overdue') AND period_end < ?",
            [$today]
        )['cnt'] ?? 0);

        return $this->view('SportsActivity.Views.dashboard', [
            'stats' => [
                'active_disciplines'   => $activeDisciplines,
                'active_facilities'    => $activeFacilities,
                'active_units'         => $activeUnits,
                'active_coaches'       => $activeCoaches,
                'total_players'        => $totalPlayers,
                'active_groups'        => $activeGroups,
                'enrolled_players'     => $enrolledPlayers,
                'today_bookings'       => $todayBookings,
                'pending_medical'      => $pendingMedical,
                'overdue_subscriptions' => $overdueSubscriptions,
            ],
            'today' => $today,
        ]);
    }
}
