<?php
declare(strict_types=1);

namespace App\Modules\Procurement\Models;

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

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

    protected static array $fillable = [
        'payment_number', 'supplier_id', 'invoice_id', 'amount', 'currency',
        'payment_method', 'bank_account_id', 'check_number', 'reference_number',
        'payment_date', 'status', 'approved_by', 'approved_at', 'completed_by',
        'completed_at', 'voided_by', 'voided_at', 'void_reason',
        'journal_entry_id', 'notes', 'branch_id',
    ];

    public static function getStatuses(): array
    {
        return [
            'draft'     => 'مسودة',
            'approved'  => 'معتمدة',
            'completed' => 'مكتملة',
            'voided'    => 'ملغاة',
        ];
    }

    public static function getPaymentMethods(): array
    {
        return [
            'cash'          => 'نقدي',
            'bank_transfer' => 'تحويل بنكي',
            'check'         => 'شيك',
            'wire'          => 'حوالة',
        ];
    }

    public static function getStatusColor(string $status): string
    {
        return match ($status) {
            'draft'     => '#6B7280',
            'approved'  => '#D97706',
            'completed' => '#059669',
            'voided'    => '#DC2626',
            default     => '#6B7280',
        };
    }

    public static function getStatusBg(string $status): string
    {
        return match ($status) {
            'draft'     => '#F3F4F6',
            'approved'  => '#FFF7ED',
            'completed' => '#ECFDF5',
            'voided'    => '#FEE2E2',
            default     => '#F3F4F6',
        };
    }

    public static function search(array $filters, int $perPage = 25, int $page = 1): array
    {
        $db = App::getInstance()->db();
        $where = 'vp.`is_archived` = 0';
        $params = [];

        if (!empty($filters['status'])) {
            $where .= ' AND vp.`status` = ?';
            $params[] = $filters['status'];
        }
        if (!empty($filters['supplier_id'])) {
            $where .= ' AND vp.`supplier_id` = ?';
            $params[] = (int) $filters['supplier_id'];
        }
        if (!empty($filters['date_from'])) {
            $where .= ' AND vp.`payment_date` >= ?';
            $params[] = $filters['date_from'];
        }
        if (!empty($filters['date_to'])) {
            $where .= ' AND vp.`payment_date` <= ?';
            $params[] = $filters['date_to'];
        }

        $countRow = $db->selectOne("SELECT COUNT(*) as cnt FROM `vendor_payments` vp WHERE {$where}", $params);
        $total = (int) ($countRow['cnt'] ?? 0);
        $offset = ($page - 1) * $perPage;

        $rows = $db->select(
            "SELECT vp.*, s.`name_ar` as supplier_name, vi.`internal_number` as invoice_number
             FROM `vendor_payments` vp
             JOIN `suppliers` s ON s.`id` = vp.`supplier_id`
             LEFT JOIN `vendor_invoices` vi ON vi.`id` = vp.`invoice_id`
             WHERE {$where}
             ORDER BY vp.`created_at` DESC
             LIMIT {$perPage} OFFSET {$offset}",
            $params
        );

        return ['data' => $rows, 'pagination' => Pagination::paginate($total, $perPage, $page)];
    }
}
