<?php
declare(strict_types=1);

namespace App\Modules\Sessions\Models;

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

class SessionAttendance extends Model
{
    protected static string $table = 'session_attendance';
    protected static string $primaryKey = 'id';
    protected static bool $timestamps = false;
    protected static bool $softDelete = false;
    protected static bool $autoTrackAuthor = true;

    protected static array $fillable = [
        'session_id',
        'player_id',
        'enrollment_id',
        'status',
        'check_in_time',
        'check_out_time',
        'is_makeup',
        'makeup_for_session_id',
        'notes',
    ];

    public static function getStatuses(): array
    {
        return [
            'present' => 'حاضر',
            'absent'  => 'غائب',
            'late'    => 'متأخر',
            'excused' => 'معتذر',
            'makeup'  => 'تعويضي',
        ];
    }

    public static function getStatusColor(string $status): string
    {
        return match ($status) {
            'present' => '#10B981',
            'absent'  => '#EF4444',
            'late'    => '#F59E0B',
            'excused' => '#6B7280',
            'makeup'  => '#7C3AED',
            default   => '#6B7280',
        };
    }

    public static function getForSession(int $sessionId): array
    {
        $db = App::getInstance()->db();
        return $db->select(
            "SELECT sa.*, p.full_name_ar AS player_name, p.phone AS player_phone
             FROM session_attendance sa
             LEFT JOIN players p ON p.id = sa.player_id
             WHERE sa.session_id = ?
             ORDER BY p.full_name_ar ASC",
            [$sessionId]
        );
    }
}
