<?php
declare(strict_types=1);

namespace App\Modules\Sports\Models;

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

class SportsMember extends Model
{
    protected static string $table = 'sports_members';
    protected static string $primaryKey = 'id';
    protected static bool $timestamps = true;
    protected static bool $softDelete = true;
    protected static bool $dispatchEvents = true;

    protected static array $fillable = [
        'member_id', 'sport_name', 'federation_name', 'federation_registration',
        'registration_date', 'years_of_service', 'highest_competitive_level',
        'is_conversion_eligible', 'conversion_requested', 'conversion_date',
        'conversion_fee', 'conversion_receipt', 'status', 'notes',
    ];

    public static function getForMember(int $memberId): ?array
    {
        $db = App::getInstance()->db();
        return $db->selectOne(
            "SELECT * FROM sports_members WHERE member_id = ? AND is_archived = 0",
            [$memberId]
        );
    }

    public static function getAllActive(): array
    {
        $db = App::getInstance()->db();
        return $db->select(
            "SELECT s.*, m.full_name_ar as member_name, m.membership_number
             FROM sports_members s
             JOIN members m ON m.id = s.member_id
             WHERE s.is_archived = 0 AND s.status = 'active'
             ORDER BY s.years_of_service DESC"
        );
    }
}