<?php
declare(strict_types=1);

namespace App\Modules\Academies\Models;

use App\Core\Model;

class Academy extends Model
{
    protected static string $table = 'academies';
    protected static string $primaryKey = 'id';
    protected static bool $timestamps = true;
    protected static bool $softDelete = true;
    protected static bool $autoTrackAuthor = true;

    protected static array $fillable = [
        'code',
        'name_ar',
        'name_en',
        'discipline_id',
        'academy_type',
        'description_ar',
        'config_json',
        'sort_order',
        '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 academy types with Arabic labels.
     */
    public static function getAcademyTypes(): array
    {
        return [
            'football'   => 'كرة قدم',
            'gymnastics' => 'جمباز',
            'swimming'   => 'سباحة',
            'combat'     => 'فنون قتالية',
            'racket'     => 'راكيت',
            'general'    => 'عام',
        ];
    }

    /**
     * Get the Arabic label for an academy type.
     */
    public static function getTypeLabel(string $type): string
    {
        $types = self::getAcademyTypes();
        return $types[$type] ?? $type;
    }

    /**
     * Get the badge color for an academy type.
     */
    public static function getTypeColor(string $type): string
    {
        $colors = [
            'football'   => '#0D7377',
            'gymnastics' => '#7C3AED',
            'swimming'   => '#2563EB',
            'combat'     => '#DC2626',
            'racket'     => '#D97706',
            'general'    => '#059669',
        ];
        return $colors[$type] ?? '#6B7280';
    }

    /**
     * Get all active academies ordered by sort_order, name_ar.
     */
    public static function allActive(): array
    {
        return static::query()
            ->where('is_active', '=', 1)
            ->orderBy('sort_order', 'ASC')
            ->orderBy('name_ar', 'ASC')
            ->get();
    }

    /**
     * Get active academies filtered by discipline.
     */
    public static function getByDiscipline(int $disciplineId): array
    {
        return static::query()
            ->where('is_active', '=', 1)
            ->where('discipline_id', '=', $disciplineId)
            ->orderBy('sort_order', 'ASC')
            ->orderBy('name_ar', 'ASC')
            ->get();
    }

    /**
     * Get active academies filtered by academy_type.
     */
    public static function getByType(string $type): array
    {
        return static::query()
            ->where('is_active', '=', 1)
            ->where('academy_type', '=', $type)
            ->orderBy('sort_order', 'ASC')
            ->orderBy('name_ar', 'ASC')
            ->get();
    }

    /**
     * Search academies with filters and pagination.
     */
    public static function search(array $filters, int $perPage = 25, int $page = 1): array
    {
        $query = static::query();

        if (!empty($filters['q'])) {
            $search = '%' . $filters['q'] . '%';
            $query = $query->whereRaw(
                '(`name_ar` LIKE ? OR `name_en` LIKE ? OR `code` LIKE ?)',
                [$search, $search, $search]
            );
        }

        if (!empty($filters['academy_type'])) {
            $query = $query->where('academy_type', '=', $filters['academy_type']);
        }

        if (!empty($filters['discipline_id'])) {
            $query = $query->where('discipline_id', '=', (int) $filters['discipline_id']);
        }

        if (isset($filters['is_active']) && $filters['is_active'] !== '') {
            $query = $query->where('is_active', '=', (int) $filters['is_active']);
        }

        $query = $query->orderBy('sort_order', 'ASC')->orderBy('name_ar', 'ASC');

        return $query->paginate($perPage, $page);
    }
}
