<?php
declare(strict_types=1);

namespace App\Modules\News\Models;

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

class NewsArticle extends Model
{
    protected static string $table = 'news_articles';
    protected static bool $softDelete = false;
    protected static array $fillable = [
        'title_ar',
        'body_ar',
        'category',
        'cover_image_path',
        'related_match_id',
        'related_facility_id',
        'is_published',
        'published_at',
        'created_by',
    ];

    /**
     * Search articles with optional filters.
     */
    public static function search(array $filters = [], int $perPage = 20, int $page = 1): array
    {
        $qb = static::query();

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

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

        if (!empty($filters['q'])) {
            $qb->where('title_ar', 'LIKE', '%' . $filters['q'] . '%');
        }

        $qb->orderBy('created_at', 'DESC');

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

    /**
     * Get category labels in Arabic.
     */
    public static function getCategoryLabels(): array
    {
        return [
            'match'        => 'مباريات',
            'facility'     => 'مرافق',
            'announcement' => 'إعلانات',
            'schedule'     => 'جداول',
            'general'      => 'عام',
        ];
    }

    /**
     * Get category color.
     */
    public static function getCategoryColor(string $category): string
    {
        return match ($category) {
            'match'        => '#DC2626',
            'facility'     => '#2563EB',
            'announcement' => '#F59E0B',
            'schedule'     => '#8B5CF6',
            'general'      => '#6B7280',
            default        => '#9CA3AF',
        };
    }
}
