<?php
declare(strict_types=1);

namespace App\Modules\Inventory\Models;

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

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

    protected static array $fillable = [
        'transfer_number',
        'from_warehouse_id',
        'to_warehouse_id',
        'status',
        'requested_by',
        'approved_by',
        'approved_at',
        'received_by',
        'received_at',
        'transfer_date',
        'notes',
    ];

    public static function getStatuses(): array
    {
        return [
            'draft'            => 'مسودة',
            'pending_approval' => 'في انتظار الموافقة',
            'approved'         => 'تمت الموافقة',
            'in_transit'       => 'قيد النقل',
            'received'         => 'تم الاستلام',
            'cancelled'        => 'ملغي',
        ];
    }

    public static function getStatusLabel(string $status): string
    {
        $statuses = self::getStatuses();
        return $statuses[$status] ?? $status;
    }

    public static function getStatusColor(string $status): string
    {
        return match ($status) {
            'draft'            => '#6B7280',
            'pending_approval' => '#D97706',
            'approved'         => '#0D7377',
            'in_transit'       => '#2563EB',
            'received'         => '#059669',
            'cancelled'        => '#DC2626',
            default            => '#6B7280',
        };
    }

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

        if (!empty($filters['status'])) {
            $where .= ' AND t.`status` = ?';
            $params[] = $filters['status'];
        }

        if (!empty($filters['from_warehouse_id'])) {
            $where .= ' AND t.`from_warehouse_id` = ?';
            $params[] = (int) $filters['from_warehouse_id'];
        }

        if (!empty($filters['to_warehouse_id'])) {
            $where .= ' AND t.`to_warehouse_id` = ?';
            $params[] = (int) $filters['to_warehouse_id'];
        }

        if (!empty($filters['date_from'])) {
            $where .= ' AND t.`transfer_date` >= ?';
            $params[] = $filters['date_from'];
        }

        if (!empty($filters['date_to'])) {
            $where .= ' AND t.`transfer_date` <= ?';
            $params[] = $filters['date_to'];
        }

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

        $rows = $db->select(
            "SELECT t.*, fw.`name_ar` as from_warehouse_name, tw.`name_ar` as to_warehouse_name
             FROM `stock_transfers` t
             JOIN `warehouses` fw ON fw.`id` = t.`from_warehouse_id`
             JOIN `warehouses` tw ON tw.`id` = t.`to_warehouse_id`
             WHERE {$where}
             ORDER BY t.`created_at` DESC
             LIMIT {$perPage} OFFSET {$offset}",
            $params
        );

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