<?php
declare(strict_types=1);

namespace App\Modules\Sessions\Models;

use App\Core\Model;
use App\Core\App;

class MakeupCredit extends Model
{
    protected static string $table = 'makeup_credits';
    protected static string $primaryKey = 'id';
    protected static bool $timestamps = false;
    protected static bool $softDelete = false;
    protected static bool $autoTrackAuthor = false;

    protected static array $fillable = [
        'player_id',
        'enrollment_id',
        'missed_session_id',
        'used_session_id',
        'status',
        'expires_at',
    ];

    public static function getStatuses(): array
    {
        return [
            'available' => 'متاح',
            'used'      => 'مستخدم',
            'expired'   => 'منتهي',
        ];
    }

    public static function getAvailableForPlayer(int $playerId): array
    {
        $db = App::getInstance()->db();
        return $db->select(
            "SELECT mc.*, ts.session_date AS missed_date, tg.name_ar AS group_name
             FROM makeup_credits mc
             LEFT JOIN training_sessions ts ON ts.id = mc.missed_session_id
             LEFT JOIN training_groups tg ON tg.id = ts.group_id
             WHERE mc.player_id = ?
               AND mc.status = 'available'
               AND (mc.expires_at IS NULL OR mc.expires_at >= CURDATE())
             ORDER BY mc.created_at DESC",
            [$playerId]
        );
    }
}
