<?php
declare(strict_types=1);

namespace App\Modules\AcademyContracts\Models;

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

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

    protected static array $fillable = [
        'settlement_number',
        'academy_id',
        'contract_id',
        'period_month',
        'total_revenue',
        'minimum_guarantee',
        'revenue_surplus',
        'revenue_shortfall',
        'club_commission',
        'academy_share',
        'net_amount',
        'penalty_amount',
        'direction',
        'status',
        'approved_by',
        'approved_at',
        'payment_id',
        'paid_at',
        'dispute_reason',
        'notes',
    ];

    /**
     * Get all statuses with Arabic labels.
     */
    public static function getStatuses(): array
    {
        return [
            'draft'            => 'مسودة',
            'pending_approval' => 'في انتظار الموافقة',
            'approved'         => 'معتمدة',
            'paid'             => 'مدفوعة',
            'disputed'         => 'متنازع عليها',
        ];
    }

    /**
     * Get all directions with Arabic labels.
     */
    public static function getDirections(): array
    {
        return [
            'club_pays_academy'  => 'النادي يدفع للأكاديمية',
            'academy_pays_club'  => 'الأكاديمية تدفع للنادي',
            'balanced'           => 'متوازن',
        ];
    }

    /**
     * Get the Arabic label for a status.
     */
    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 = [
            'draft'            => '#9CA3AF',
            'pending_approval' => '#D97706',
            'approved'         => '#059669',
            'paid'             => '#2563EB',
            'disputed'         => '#DC2626',
        ];
        return $colors[$status] ?? '#6B7280';
    }

    /**
     * Get the Arabic label for a direction.
     */
    public static function getDirectionLabel(string $direction): string
    {
        $directions = self::getDirections();
        return $directions[$direction] ?? $direction;
    }

    /**
     * Get the color for a direction.
     */
    public static function getDirectionColor(string $direction): string
    {
        $colors = [
            'club_pays_academy' => '#059669',
            'academy_pays_club' => '#DC2626',
            'balanced'          => '#6B7280',
        ];
        return $colors[$direction] ?? '#6B7280';
    }

    /**
     * Search settlements with filters and pagination.
     */
    public static function search(array $filters, int $perPage = 25, int $page = 1): array
    {
        $query = static::query()
            ->leftJoin('academies', '`academy_settlements`.`academy_id` = `academies`.`id`')
            ->leftJoin('academy_contracts', '`academy_settlements`.`contract_id` = `academy_contracts`.`id`')
            ->select('`academy_settlements`.*, `academies`.`name_ar` as academy_name, `academy_contracts`.`contract_number`');

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

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

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

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

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

        $query = $query->orderBy('academy_settlements.period_month', 'DESC')
                       ->orderBy('academy_settlements.created_at', 'DESC');

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