<?php
declare(strict_types=1);

namespace App\Modules\Disciplines\Models;

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

class SportDiscipline extends Model
{
    protected static string $table = 'sport_disciplines';
    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',
        'category',
        'icon',
        '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 : [];
    }

    /**
     * Extract age groups from the config.
     */
    public function getAgeGroups(): array
    {
        $config = $this->getConfig();
        return $config['age_groups'] ?? [];
    }

    /**
     * Extract skill levels from the config.
     */
    public function getSkillLevels(): array
    {
        $config = $this->getConfig();
        return $config['skill_levels'] ?? [];
    }

    /**
     * Get all discipline categories with Arabic labels.
     */
    public static function getCategories(): array
    {
        return [
            'individual' => 'الفردية',
            'team'       => 'الجماعية',
            'racket'     => 'الراكيت',
            'leisure'    => 'الترفيهية',
            'emerging'   => 'الناشئة',
        ];
    }

    /**
     * Get the Arabic label for a category key.
     */
    public static function getCategoryLabel(string $category): string
    {
        $categories = self::getCategories();
        return $categories[$category] ?? $category;
    }

    /**
     * Get the badge color for a category.
     */
    public static function getCategoryColor(string $category): string
    {
        $colors = [
            'individual' => '#0D7377',
            'team'       => '#0284C7',
            'racket'     => '#7C3AED',
            'leisure'    => '#D97706',
            'emerging'   => '#059669',
        ];
        return $colors[$category] ?? '#6B7280';
    }

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

    /**
     * Get disciplines by category.
     */
    public static function getByCategory(string $category): array
    {
        return static::query()
            ->where('is_active', '=', 1)
            ->where('category', '=', $category)
            ->orderBy('sort_order', 'ASC')
            ->orderBy('name_ar', 'ASC')
            ->get();
    }

    /**
     * Search disciplines 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['category'])) {
            $query = $query->where('category', '=', $filters['category']);
        }

        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);
    }
}
