<?php
declare(strict_types=1);

namespace App\Modules\SportsActivity\Models;

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

class Player extends Model
{
    protected static string $table = 'sa_players';
    protected static string $primaryKey = 'id';
    protected static bool $timestamps = true;
    protected static bool $softDelete = true;
    protected static bool $dispatchEvents = true;
    protected static bool $autoTrackAuthor = true;

    protected static array $fillable = [
        'player_type',
        'member_id',
        'registration_serial',
        'full_name_ar',
        'full_name_en',
        'national_id',
        'date_of_birth',
        'gender',
        'phone',
        'email',
        'guardian_name',
        'guardian_phone',
        'guardian_national_id',
        'guardian_relationship',
        'photo_path',
        'medical_status',
        'medical_expiry_date',
        'card_status',
        'registration_fee_paid',
        'notes',
        'branch_id',
    ];

    /**
     * Get medical status options with Arabic labels.
     */
    public static function getMedicalStatusOptions(): array
    {
        return [
            'pending'     => 'قيد المراجعة',
            'fit'         => 'لائق',
            'conditional' => 'لائق بشروط',
            'unfit'       => 'غير لائق',
            'expired'     => 'منتهي',
        ];
    }

    /**
     * Get player type options with Arabic labels.
     */
    public static function getPlayerTypeOptions(): array
    {
        return [
            'member'     => 'عضو',
            'non_member' => 'غير عضو',
        ];
    }

    /**
     * Get card status options with Arabic labels.
     */
    public static function getCardStatusOptions(): array
    {
        return [
            'inactive'  => 'غير مفعل',
            'active'    => 'مفعل',
            'suspended' => 'موقوف',
            'revoked'   => 'ملغى',
        ];
    }

    /**
     * Get all documents for this player.
     */
    public function getDocuments(): array
    {
        $db = App::getInstance()->db();
        return $db->select(
            "SELECT * FROM `sa_player_documents`
             WHERE `player_id` = ?
             ORDER BY `created_at` DESC",
            [$this->id]
        );
    }

    /**
     * Get all active (non-archived) players.
     */
    public static function getActive(): array
    {
        return static::query()
            ->where('is_archived', '=', 0)
            ->orderBy('full_name_ar', 'ASC')
            ->get();
    }
}
