<?php
declare(strict_types=1);

namespace App\Modules\TrainerPortal\Controllers\Api;

use App\Core\ApiController;
use App\Core\App;
use App\Core\Request;
use App\Core\Response;

final class CoachScheduleController extends ApiController
{
    public function __construct(Request $request)
    {
        parent::__construct();
        $this->request = $request;
    }

    public function weeklySchedule(Request $request): Response
    {
        $coach = App::getInstance()->currentCoach();
        $db = App::getInstance()->db();

        $weekStart = $request->get('week', date('Y-m-d', strtotime('monday this week')));
        $weekEnd = date('Y-m-d', strtotime($weekStart . ' +6 days'));

        $sessions = $db->select(
            "SELECT ts.id, ts.session_date, ts.start_time, ts.end_time, ts.session_type, ts.status,
                    ts.players_expected, ts.players_attended,
                    tg.name_ar as group_name, tg.group_type,
                    f.name_ar as facility_name,
                    sd.name_ar as discipline_name
             FROM training_sessions ts
             INNER JOIN training_groups tg ON tg.id = ts.group_id
             LEFT JOIN facilities f ON f.id = ts.facility_id
             LEFT JOIN sport_disciplines sd ON sd.id = tg.discipline_id
             WHERE ts.coach_id = ? AND ts.session_date BETWEEN ? AND ?
             ORDER BY ts.session_date, ts.start_time",
            [$coach->id, $weekStart, $weekEnd]
        );

        return $this->successResponse($sessions);
    }

    public function sessions(Request $request): Response
    {
        $coach = App::getInstance()->currentCoach();
        $db = App::getInstance()->db();

        $from = $request->get('from', date('Y-m-d'));
        $to = $request->get('to', date('Y-m-d', strtotime('+30 days')));

        $sessions = $db->select(
            "SELECT ts.id, ts.session_date, ts.start_time, ts.end_time, ts.session_type, ts.status,
                    ts.players_expected, ts.players_attended,
                    tg.name_ar as group_name, tg.group_type,
                    f.name_ar as facility_name
             FROM training_sessions ts
             INNER JOIN training_groups tg ON tg.id = ts.group_id
             LEFT JOIN facilities f ON f.id = ts.facility_id
             WHERE ts.coach_id = ? AND ts.session_date BETWEEN ? AND ?
             ORDER BY ts.session_date, ts.start_time",
            [$coach->id, $from, $to]
        );

        return $this->successResponse($sessions);
    }

    public function groups(Request $request): Response
    {
        $coach = App::getInstance()->currentCoach();
        $db = App::getInstance()->db();

        $groups = $db->select(
            "SELECT tg.id, tg.name_ar, tg.group_type, tg.day_of_week, tg.start_time, tg.end_time,
                    tg.current_count, tg.max_capacity, tg.gender_restriction, tg.age_from, tg.age_to,
                    f.name_ar as facility_name,
                    sd.name_ar as discipline_name,
                    al.name_ar as level_name
             FROM training_groups tg
             LEFT JOIN facilities f ON f.id = tg.facility_id
             LEFT JOIN sport_disciplines sd ON sd.id = tg.discipline_id
             LEFT JOIN academy_levels al ON al.id = tg.level_id
             WHERE tg.coach_id = ? AND tg.is_active = 1 AND tg.is_archived = 0
             ORDER BY tg.day_of_week, tg.start_time",
            [$coach->id]
        );

        return $this->successResponse($groups);
    }

    public function groupPlayers(Request $request, string $id): Response
    {
        $coach = App::getInstance()->currentCoach();
        $db = App::getInstance()->db();

        $group = $db->selectOne(
            "SELECT id FROM training_groups WHERE id = ? AND coach_id = ?",
            [(int) $id, $coach->id]
        );

        if (!$group) {
            return $this->errorResponse('المجموعة غير موجودة أو ليست من مجموعاتك', 404);
        }

        $players = $db->select(
            "SELECT p.id, p.name_ar, p.phone, p.date_of_birth, p.gender,
                    p.medical_status, p.medical_expiry_date,
                    gm.joined_at, gm.status as membership_status
             FROM group_memberships gm
             INNER JOIN players p ON p.id = gm.player_id
             WHERE gm.group_id = ? AND gm.status = 'active'
             ORDER BY p.name_ar",
            [(int) $id]
        );

        return $this->successResponse($players);
    }
}
