<?php
declare(strict_types=1);

namespace App\Modules\SportsActivity\Models;

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

class Academy extends Model
{
    protected static string $table = 'sa_academies';
    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 = [
        'code',
        'name_ar',
        'name_en',
        'discipline_id',
        'academy_type',
        'contact_person',
        'contact_phone',
        'contact_email',
        'description_ar',
        'logo_path',
        'is_active',
        'branch_id',
    ];

    /**
     * Get academy type options with Arabic labels.
     */
    public static function getAcademyTypeOptions(): array
    {
        return [
            'internal' => 'داخلية',
            'external' => 'خارجية',
        ];
    }

    /**
     * Get all contracts for this academy.
     */
    public function getContracts(): array
    {
        $db = App::getInstance()->db();
        return $db->select(
            "SELECT * FROM `sa_academy_contracts`
             WHERE `academy_id` = ? AND `is_archived` = 0
             ORDER BY `start_date` DESC",
            [$this->id]
        );
    }

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