<?php
declare(strict_types=1);

namespace App\Modules\Academies\Models;

use App\Core\Model;

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

    protected static array $fillable = [
        'academy_id',
        'code',
        'name_ar',
        'name_en',
        'level_order',
        'age_min',
        'age_max',
        'max_capacity',
        'config_json',
        'is_active',
    ];

    /**
     * Decode config_json into an associative array.
     */
    public function getConfig(): array
    {
        $raw = $this->config_json;
        if (empty($raw)) {
            return [];
        }
        $decoded = json_decode($raw, true);
        return is_array($decoded) ? $decoded : [];
    }

    /**
     * Get all levels for a given academy, ordered by level_order.
     */
    public static function getForAcademy(int $academyId): array
    {
        return static::query()
            ->where('academy_id', '=', $academyId)
            ->orderBy('level_order', 'ASC')
            ->get();
    }
}
