<?php
declare(strict_types=1);

namespace App\Modules\PlayerAffairs\Models;

use App\Core\Model;

class AcademyEnrollment extends Model
{
    protected static string $table = 'academy_enrollments';
    protected static string $primaryKey = 'id';
    protected static bool $timestamps = true;
    protected static bool $softDelete = false;

    protected static array $fillable = [
        'player_id',
        'academy_id',
        'level_id',
        'schedule_id',
        'season',
        'enrolled_at',
        'enrollment_day',
        'status',
        'promoted_from_id',
        'dropped_at',
        'dropped_reason',
        'created_by',
    ];

    /**
     * Get enrollment statuses with Arabic labels.
     */
    public static function getStatuses(): array
    {
        return [
            'active'    => 'فعال',
            'suspended' => 'موقوف',
            'dropped'   => 'منسحب',
            'promoted'  => 'ترقية',
            'completed' => 'مكتمل',
        ];
    }

    /**
     * Get the Arabic label for a status.
     */
    public static function getStatusLabel(string $status): string
    {
        $statuses = self::getStatuses();
        return $statuses[$status] ?? $status;
    }

    /**
     * Get the badge color for a status.
     */
    public static function getStatusColor(string $status): string
    {
        $colors = [
            'active'    => '#059669',
            'suspended' => '#D97706',
            'dropped'   => '#DC2626',
            'promoted'  => '#7C3AED',
            'completed' => '#0284C7',
        ];
        return $colors[$status] ?? '#6B7280';
    }

    /**
     * Get all enrollments for a specific player with academy and level names.
     */
    public static function getForPlayer(int $playerId): array
    {
        $db = \App\Core\App::getInstance()->db();
        return $db->select(
            "SELECT ae.*, a.name_ar AS academy_name, al.name_ar AS level_name
             FROM academy_enrollments ae
             LEFT JOIN academies a ON a.id = ae.academy_id
             LEFT JOIN academy_levels al ON al.id = ae.level_id
             WHERE ae.player_id = ?
             ORDER BY ae.created_at DESC",
            [$playerId]
        );
    }

    /**
     * Get all enrollments for a specific academy.
     */
    public static function getForAcademy(int $academyId): array
    {
        return static::query()
            ->where('academy_id', '=', $academyId)
            ->orderBy('created_at', 'DESC')
            ->get();
    }
}
