<?php
declare(strict_types=1);

namespace App\Modules\SportsActivity\Controllers\Api;

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

class ActivityBrowserApiController extends Controller
{
    public function disciplines(Request $request): Response
    {
        $db = App::getInstance()->db();

        $disciplines = $db->select(
            "SELECT d.id, d.code, d.name_ar, d.icon, d.category,
                    (SELECT COUNT(*) FROM sa_programs p WHERE p.discipline_id = d.id AND p.is_archived = 0) as program_count
             FROM sa_disciplines d
             WHERE d.is_active = 1 AND d.is_archived = 0
             ORDER BY d.sort_order ASC, d.name_ar ASC",
            []
        );

        return $this->json(['success' => true, 'disciplines' => $disciplines]);
    }

    public function programs(Request $request): Response
    {
        $disciplineId = (int) $request->get('discipline_id', 0);
        if ($disciplineId === 0) {
            return $this->json(['success' => false, 'error' => 'discipline_id مطلوب']);
        }

        $db = App::getInstance()->db();

        $programs = $db->select(
            "SELECT p.id, p.code, p.name_ar, p.program_type, p.skill_level,
                    p.age_from, p.age_to, p.gender_restriction, p.sessions_per_week,
                    p.session_duration_minutes,
                    (SELECT COUNT(*) FROM sa_groups g WHERE g.program_id = p.id AND g.is_archived = 0 AND g.status = 'active') as group_count
             FROM sa_programs p
             WHERE p.discipline_id = ? AND p.is_archived = 0
             ORDER BY p.name_ar ASC",
            [$disciplineId]
        );

        return $this->json(['success' => true, 'programs' => $programs]);
    }

    public function groups(Request $request): Response
    {
        $programId = (int) $request->get('program_id', 0);
        if ($programId === 0) {
            return $this->json(['success' => false, 'error' => 'program_id مطلوب']);
        }

        $db = App::getInstance()->db();

        $groups = $db->select(
            "SELECT g.id, g.code, g.name_ar, g.current_count, g.max_capacity, g.status,
                    g.monthly_fee_member, g.monthly_fee_nonmember,
                    c.full_name_ar as coach_name
             FROM sa_groups g
             LEFT JOIN sa_coaches c ON c.id = g.coach_id
             WHERE g.program_id = ? AND g.is_archived = 0
             ORDER BY g.name_ar ASC",
            [$programId]
        );

        return $this->json(['success' => true, 'groups' => $groups]);
    }

    public function programsByDiscipline(Request $request): Response
    {
        $disciplineId = (int) $request->get('discipline_id', 0);
        $db = App::getInstance()->db();

        $where = "p.is_archived = 0";
        $params = [];

        if ($disciplineId > 0) {
            $where .= " AND p.discipline_id = ?";
            $params[] = $disciplineId;
        }

        $programs = $db->select(
            "SELECT p.id, p.name_ar, p.age_from, p.age_to, p.sessions_per_week,
                    p.gender_restriction, p.session_duration_minutes
             FROM sa_programs p
             WHERE {$where}
             ORDER BY p.name_ar ASC",
            $params
        );

        return $this->json(['success' => true, 'programs' => $programs]);
    }

    public function coachesByDiscipline(Request $request): Response
    {
        $disciplineId = (int) $request->get('discipline_id', 0);
        $db = App::getInstance()->db();

        if ($disciplineId > 0) {
            $coaches = $db->select(
                "SELECT c.id, c.full_name_ar as name_ar, c.gender
                 FROM sa_coaches c
                 INNER JOIN sa_coach_disciplines cd ON cd.coach_id = c.id AND cd.discipline_id = ?
                 WHERE c.is_archived = 0
                 ORDER BY c.full_name_ar ASC",
                [$disciplineId]
            );
        } else {
            $coaches = $db->select(
                "SELECT id, full_name_ar as name_ar, gender FROM sa_coaches WHERE is_archived = 0 ORDER BY full_name_ar ASC",
                []
            );
        }

        return $this->json(['success' => true, 'coaches' => $coaches]);
    }
}
