<?php
declare(strict_types=1);

namespace App\Modules\PlayerAffairs\Models;

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

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

    protected static array $fillable = [
        'discipline_id',
        'criterion_name_ar',
        'criterion_name_en',
        'description_ar',
        'max_score',
        'weight',
        'sort_order',
        'is_active',
    ];

    /**
     * Get all active criteria for a specific discipline, ordered by sort_order.
     */
    public static function getForDiscipline(int $disciplineId): array
    {
        $db = App::getInstance()->db();
        return $db->select(
            "SELECT * FROM evaluation_criteria
             WHERE discipline_id = ? AND is_active = 1
             ORDER BY sort_order ASC, id ASC",
            [$disciplineId]
        );
    }

    /**
     * Get all active criteria across all disciplines.
     */
    public static function allActive(): array
    {
        $db = App::getInstance()->db();
        return $db->select(
            "SELECT ec.*, d.name_ar AS discipline_name
             FROM evaluation_criteria ec
             LEFT JOIN sport_disciplines d ON d.id = ec.discipline_id
             WHERE ec.is_active = 1
             ORDER BY ec.discipline_id ASC, ec.sort_order ASC, ec.id ASC",
            []
        );
    }
}
