<?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 CoachProfileController extends ApiController
{
    public function __construct(Request $request)
    {
        parent::__construct();
        $this->request = $request;
    }

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

        $full = $db->selectOne(
            "SELECT id, name_ar, name_en, phone, email, employment_type, payment_model,
                    session_rate, monthly_salary, certifications_json, session_types_json,
                    is_active, created_at
             FROM coaches WHERE id = ?",
            [$coach->id]
        );

        $disciplines = $db->select(
            "SELECT cd.discipline_id, sd.name_ar as discipline_name
             FROM coach_disciplines cd
             INNER JOIN sport_disciplines sd ON sd.id = cd.discipline_id
             WHERE cd.coach_id = ?",
            [$coach->id]
        );

        $full['disciplines'] = $disciplines;

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

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

        $assignments = $db->select(
            "SELECT caa.id, caa.academy_id, caa.role, caa.assigned_at,
                    a.name_ar as academy_name, a.description_ar,
                    ac.id as contract_id, ac.contract_type, ac.revenue_share_percent,
                    ac.start_date, ac.end_date, ac.status as contract_status, ac.contract_image_path
             FROM coach_academy_assignments caa
             INNER JOIN academies a ON a.id = caa.academy_id
             LEFT JOIN academy_contracts ac ON ac.academy_id = a.id AND ac.status = 'active'
             WHERE caa.coach_id = ? AND caa.is_active = 1",
            [$coach->id]
        );

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