<?php
declare(strict_types=1);

namespace App\Modules\ActivitySubscriptions\Models;

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

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

    protected static array $fillable = [
        'player_id',
        'enrollment_id',
        'discipline_id',
        'subscription_month',
        'player_type',
        'base_rate',
        'is_half_month',
        'applied_rate',
        'discount',
        'total_amount',
        'payment_id',
        'status',
        'due_date',
        'paid_at',
        'revoked_at',
        'exempted_by',
        'exemption_reason',
        'notes',
        'created_by',
    ];

    /**
     * Get all subscription statuses with Arabic labels.
     */
    public static function getStatuses(): array
    {
        return [
            'pending'  => 'مستحق',
            'paid'     => 'مدفوع',
            'overdue'  => 'متأخر',
            'exempted' => 'معفى',
            'revoked'  => 'ملغى',
        ];
    }

    /**
     * Get the Arabic label for a status key.
     */
    public static function getStatusLabel(string $status): string
    {
        $statuses = self::getStatuses();
        return $statuses[$status] ?? $status;
    }

    /**
     * Get the badge color for a status.
     */
    public static function getStatusColor(string $status): string
    {
        $colors = [
            'pending'  => '#D97706',
            'paid'     => '#059669',
            'overdue'  => '#DC2626',
            'exempted' => '#7C3AED',
            'revoked'  => '#6B7280',
        ];
        return $colors[$status] ?? '#6B7280';
    }

    /**
     * Search activity subscriptions with filters and pagination.
     */
    public static function search(array $filters, int $perPage = 25, int $page = 1): array
    {
        $query = static::query()
            ->leftJoin('players', '`activity_subscriptions`.`player_id` = `players`.`id`')
            ->leftJoin('sport_disciplines', '`activity_subscriptions`.`discipline_id` = `sport_disciplines`.`id`')
            ->select('`activity_subscriptions`.*, `players`.`full_name_ar` as player_name, `sport_disciplines`.`name_ar` as discipline_name');

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

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

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

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

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

        $query = $query->orderBy('subscription_month', 'DESC')->orderBy('players.full_name_ar', 'ASC');

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

    /**
     * Get all subscriptions for a specific player, ordered by month descending.
     */
    public static function getForPlayer(int $playerId): array
    {
        return static::query()
            ->leftJoin('sport_disciplines', '`activity_subscriptions`.`discipline_id` = `sport_disciplines`.`id`')
            ->select('`activity_subscriptions`.*, `sport_disciplines`.`name_ar` as discipline_name')
            ->where('player_id', '=', $playerId)
            ->orderBy('subscription_month', 'DESC')
            ->get();
    }
}
