<?php

namespace App\Modules\Inventory\Services;

use App\Modules\Inventory\Models\Inventory;
use App\Modules\Inventory\Models\InventoryTransferOrder;
use App\Modules\Inventory\Models\InventoryTransferItem;
use App\Modules\Inventory\Models\Warehouse;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\ValidationException;

class TransferService
{
    /**
     * 建立調撥單草稿
     */
    public function createOrder(int $fromWarehouseId, int $toWarehouseId, ?string $remarks, int $userId, ?int $transitWarehouseId = null): InventoryTransferOrder
    {
        // 若未指定在途倉，嘗試使用來源倉庫的預設在途倉 (一次性設定)
        if (is_null($transitWarehouseId)) {
            $fromWarehouse = Warehouse::find($fromWarehouseId);
            if ($fromWarehouse && $fromWarehouse->default_transit_warehouse_id) {
                $transitWarehouseId = $fromWarehouse->default_transit_warehouse_id;
            }
        }

        return InventoryTransferOrder::create([
            'from_warehouse_id' => $fromWarehouseId,
            'to_warehouse_id' => $toWarehouseId,
            'transit_warehouse_id' => $transitWarehouseId,
            'status' => 'draft',
            'remarks' => $remarks,
            'created_by' => $userId,
        ]);
    }

    /**
     * 更新調撥單明細 (支援精確 Diff 與自動日誌整合)
     */
    public function updateItems(InventoryTransferOrder $order, array $itemsData): bool
    {
        return DB::transaction(function () use ($order, $itemsData) {
            $oldItemsMap = $order->items->mapWithKeys(function ($item) {
                $key = $item->product_id . '_' . ($item->batch_number ?? '');
                return [$key => $item];
            });

            $diff = [
                'added' => [],
                'removed' => [],
                'updated' => [],
            ];

            $order->items()->delete();
            $newItemsKeys = [];

            foreach ($itemsData as $data) {
                $key = $data['product_id'] . '_' . ($data['batch_number'] ?? '');
                $newItemsKeys[] = $key;

                $item = $order->items()->create([
                    'product_id' => $data['product_id'],
                    'batch_number' => $data['batch_number'] ?? null,
                    'quantity' => $data['quantity'],
                    'position' => $data['position'] ?? null,
                    'notes' => $data['notes'] ?? null,
                ]);
                $item->load('product');

                if ($oldItemsMap->has($key)) {
                    $oldItem = $oldItemsMap->get($key);
                    if ((float)$oldItem->quantity !== (float)$data['quantity'] ||
                        $oldItem->notes !== ($data['notes'] ?? null) ||
                        $oldItem->position !== ($data['position'] ?? null)) {
                        
                        $diff['updated'][] = [
                            'product_name' => $item->product->name,
                            'old' => [
                                'quantity' => (float)$oldItem->quantity,
                                'position' => $oldItem->position,
                                'notes' => $oldItem->notes,
                            ],
                            'new' => [
                                'quantity' => (float)$data['quantity'],
                                'position' => $item->position,
                                'notes' => $item->notes,
                            ]
                        ];
                    }
                } else {
                    $diff['updated'][] = [
                        'product_name' => $item->product->name,
                        'old' => [
                            'quantity' => 0,
                            'notes' => null,
                        ],
                        'new' => [
                            'quantity' => (float)$item->quantity,
                            'notes' => $item->notes,
                        ]
                    ];
                }
            }

            foreach ($oldItemsMap as $key => $oldItem) {
                if (!in_array($key, $newItemsKeys)) {
                    $diff['removed'][] = [
                        'product_name' => $oldItem->product->name,
                        'old' => [
                            'quantity' => (float)$oldItem->quantity,
                            'notes' => $oldItem->notes,
                        ]
                    ];
                }
            }

            $hasChanged = !empty($diff['added']) || !empty($diff['removed']) || !empty($diff['updated']);
            if ($hasChanged) {
                 $order->activityProperties['items_diff'] = $diff;
            }

            return $hasChanged;
        });
    }

    /**
     * 出貨 (Dispatch) - 根據是否有在途倉決定流程
     *
     * 有在途倉：來源倉扣除 → 在途倉增加，狀態改為 dispatched
     * 無在途倉：來源倉扣除 → 目的倉增加，狀態改為 completed（維持原有邏輯）
     */
    public function dispatch(InventoryTransferOrder $order, int $userId): void
    {
        $order->load('items.product');

        DB::transaction(function () use ($order, $userId) {
            $fromWarehouse = $order->fromWarehouse;
            $hasTransit = !empty($order->transit_warehouse_id);

            $targetWarehouseId = $hasTransit ? $order->transit_warehouse_id : $order->to_warehouse_id;
            $targetWarehouse = $hasTransit ? $order->transitWarehouse : $order->toWarehouse;

            $outType = '調撥出庫';
            $inType = $hasTransit ? '在途入庫' : '調撥入庫';

            foreach ($order->items as $item) {
                if ($item->quantity <= 0) continue;

                // 1. 處理來源倉 (扣除)
                $sourceInventory = Inventory::where('warehouse_id', $order->from_warehouse_id)
                    ->where('product_id', $item->product_id)
                    ->where('batch_number', $item->batch_number)
                    ->first();

                if (!$sourceInventory || $sourceInventory->quantity < $item->quantity) {
                    $availableQty = $sourceInventory->quantity ?? 0;
                    $shortageQty = $item->quantity - $availableQty;
                    throw ValidationException::withMessages([
                        'items' => ["商品 {$item->product->name} (批號: {$item->batch_number}) 在來源倉庫存不足。現有庫存：{$availableQty}，尚欠：{$shortageQty}。"],
                    ]);
                }

                $oldSourceQty = $sourceInventory->quantity;
                $newSourceQty = $oldSourceQty - $item->quantity;

                $item->update(['snapshot_quantity' => $oldSourceQty]);
                
                $sourceInventory->quantity = $newSourceQty;
                $sourceInventory->total_value = $sourceInventory->quantity * $sourceInventory->unit_cost;
                $sourceInventory->save();

                $sourceInventory->transactions()->create([
                    'type' => $outType,
                    'quantity' => -$item->quantity,
                    'unit_cost' => $sourceInventory->unit_cost,
                    'balance_before' => $oldSourceQty,
                    'balance_after' => $newSourceQty,
                    'reason' => "調撥單 {$order->doc_no} 至 {$targetWarehouse->name}",
                    'actual_time' => now(),
                    'user_id' => $userId,
                ]);

                // 2. 處理目的倉/在途倉 (增加)
                $targetInventory = Inventory::firstOrCreate(
                    [
                        'warehouse_id' => $targetWarehouseId,
                        'product_id' => $item->product_id,
                        'batch_number' => $item->batch_number,
                        'location' => $hasTransit ? null : ($item->position ?? null),
                    ],
                    [
                        'quantity' => 0,
                        'unit_cost' => $sourceInventory->unit_cost,
                        'total_value' => 0,
                        'expiry_date' => $sourceInventory->expiry_date,
                        'quality_status' => $sourceInventory->quality_status,
                        'origin_country' => $sourceInventory->origin_country,
                    ]
                );
                
                if ($targetInventory->wasRecentlyCreated && $targetInventory->unit_cost == 0) {
                     $targetInventory->unit_cost = $sourceInventory->unit_cost;
                }

                $oldTargetQty = $targetInventory->quantity;
                $newTargetQty = $oldTargetQty + $item->quantity;

                $targetInventory->quantity = $newTargetQty;
                $targetInventory->total_value = $targetInventory->quantity * $targetInventory->unit_cost;
                $targetInventory->save();

                $targetInventory->transactions()->create([
                    'type' => $inType,
                    'quantity' => $item->quantity,
                    'unit_cost' => $targetInventory->unit_cost,
                    'balance_before' => $oldTargetQty,
                    'balance_after' => $newTargetQty,
                    'reason' => "調撥單 {$order->doc_no} 來自 {$fromWarehouse->name}",
                    'actual_time' => now(),
                    'user_id' => $userId,
                ]);
            }

            if ($hasTransit) {
                $order->status = 'dispatched';
                $order->dispatched_at = now();
                $order->dispatched_by = $userId;
            } else {
                $order->status = 'completed';
                $order->posted_at = now();
                $order->posted_by = $userId;
            }
            $order->save();
        });
    }

    /**
     * 收貨確認 (Receive) - 在途倉扣除 → 目的倉增加
     * 僅適用於有在途倉且狀態為 dispatched 的調撥單
     */
    public function receive(InventoryTransferOrder $order, int $userId): void
    {
        if ($order->status !== 'dispatched') {
            throw new \Exception('僅能對已出貨的調撥單進行收貨確認');
        }

        if (empty($order->transit_warehouse_id)) {
            throw new \Exception('此調撥單未設定在途倉庫');
        }

        $order->load('items.product');

        DB::transaction(function () use ($order, $userId) {
            $transitWarehouse = $order->transitWarehouse;
            $toWarehouse = $order->toWarehouse;

            foreach ($order->items as $item) {
                if ($item->quantity <= 0) continue;

                // 1. 在途倉扣除
                $transitInventory = Inventory::where('warehouse_id', $order->transit_warehouse_id)
                    ->where('product_id', $item->product_id)
                    ->where('batch_number', $item->batch_number)
                    ->first();

                if (!$transitInventory || $transitInventory->quantity < $item->quantity) {
                    $availableQty = $transitInventory->quantity ?? 0;
                    throw ValidationException::withMessages([
                        'items' => ["商品 {$item->product->name} 在途倉庫存不足。現有：{$availableQty}，需要：{$item->quantity}。"],
                    ]);
                }

                $oldTransitQty = $transitInventory->quantity;
                $newTransitQty = $oldTransitQty - $item->quantity;

                $transitInventory->quantity = $newTransitQty;
                $transitInventory->total_value = $transitInventory->quantity * $transitInventory->unit_cost;
                $transitInventory->save();

                $transitInventory->transactions()->create([
                    'type' => '在途出庫',
                    'quantity' => -$item->quantity,
                    'unit_cost' => $transitInventory->unit_cost,
                    'balance_before' => $oldTransitQty,
                    'balance_after' => $newTransitQty,
                    'reason' => "調撥單 {$order->doc_no} 配送至 {$toWarehouse->name}",
                    'actual_time' => now(),
                    'user_id' => $userId,
                ]);

                // 2. 目的倉增加
                $targetInventory = Inventory::firstOrCreate(
                    [
                        'warehouse_id' => $order->to_warehouse_id,
                        'product_id' => $item->product_id,
                        'batch_number' => $item->batch_number,
                        'location' => $item->position,
                    ],
                    [
                        'quantity' => 0,
                        'unit_cost' => $transitInventory->unit_cost,
                        'total_value' => 0,
                        'expiry_date' => $transitInventory->expiry_date,
                        'quality_status' => $transitInventory->quality_status,
                        'origin_country' => $transitInventory->origin_country,
                    ]
                );

                if ($targetInventory->wasRecentlyCreated && $targetInventory->unit_cost == 0) {
                    $targetInventory->unit_cost = $transitInventory->unit_cost;
                }

                $oldTargetQty = $targetInventory->quantity;
                $newTargetQty = $oldTargetQty + $item->quantity;

                $targetInventory->quantity = $newTargetQty;
                $targetInventory->total_value = $targetInventory->quantity * $targetInventory->unit_cost;
                $targetInventory->save();

                $targetInventory->transactions()->create([
                    'type' => '調撥入庫',
                    'quantity' => $item->quantity,
                    'unit_cost' => $targetInventory->unit_cost,
                    'balance_before' => $oldTargetQty,
                    'balance_after' => $newTargetQty,
                    'reason' => "調撥單 {$order->doc_no} 來自 {$transitWarehouse->name}",
                    'actual_time' => now(),
                    'user_id' => $userId,
                ]);
            }

            $order->status = 'completed';
            $order->posted_at = now();
            $order->posted_by = $userId;
            $order->received_at = now();
            $order->received_by = $userId;
            $order->save();
        });
    }

    /**
     * 作廢 (Void) - 僅限草稿狀態
     */
    public function void(InventoryTransferOrder $order, int $userId): void
    {
        if ($order->status !== 'draft') {
             throw new \Exception('只能作廢草稿狀態的單據');
        }
        $order->update([
             'status' => 'voided',
             'updated_by' => $userId
        ]);
    }
}
