<?php

namespace App\Http\Controllers;

use App\Models\Inventory;
use App\Models\Warehouse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\ValidationException;

class TransferOrderController extends Controller
{
    /**
     * 儲存撥補單（建立調撥單並執行庫存轉移）
     */
    public function store(Request $request)
    {
        $validated = $request->validate([
            'sourceWarehouseId' => 'required|exists:warehouses,id',
            'targetWarehouseId' => 'required|exists:warehouses,id|different:sourceWarehouseId',
            'productId' => 'required|exists:products,id',
            'quantity' => 'required|numeric|min:0.01',
            'transferDate' => 'required|date',
            'status' => 'required|in:待處理,處理中,已完成,已取消', // 目前僅支援立即完成或單純記錄
            'notes' => 'nullable|string',
            'batchNumber' => 'nullable|string', // 暫時接收，雖然 DB 可能沒存
        ]);

        return DB::transaction(function () use ($validated) {
            // 1. 檢查來源倉庫庫存
            $sourceInventory = Inventory::where('warehouse_id', $validated['sourceWarehouseId'])
                ->where('product_id', $validated['productId'])
                ->first();

            if (!$sourceInventory || $sourceInventory->quantity < $validated['quantity']) {
                throw ValidationException::withMessages([
                    'quantity' => ['來源倉庫庫存不足'],
                ]);
            }

            // 2. 獲取或建立目標倉庫庫存
            $targetInventory = Inventory::firstOrCreate(
                [
                    'warehouse_id' => $validated['targetWarehouseId'],
                    'product_id' => $validated['productId'],
                ],
                [
                    'quantity' => 0,
                    'safety_stock' => null, // 預設為 null (未設定)，而非 0
                ]
            );

            $sourceWarehouse = Warehouse::find($validated['sourceWarehouseId']);
            $targetWarehouse = Warehouse::find($validated['targetWarehouseId']);

            // 3. 執行庫存轉移 (扣除來源)
            $oldSourceQty = $sourceInventory->quantity;
            $newSourceQty = $oldSourceQty - $validated['quantity'];
            
            // 設定活動紀錄原因
            $sourceInventory->activityLogReason = "撥補出庫 至 {$targetWarehouse->name}";
            $sourceInventory->update(['quantity' => $newSourceQty]);

            // 記錄來源異動
            $sourceInventory->transactions()->create([
                'type' => '撥補出庫',
                'quantity' => -$validated['quantity'],
                'balance_before' => $oldSourceQty,
                'balance_after' => $newSourceQty,
                'reason' => "撥補至 {$targetWarehouse->name}" . ($validated['notes'] ? " ({$validated['notes']})" : ""),
                'actual_time' => $validated['transferDate'],
                'user_id' => auth()->id(),
            ]);

            // 4. 執行庫存轉移 (增加目標)
            $oldTargetQty = $targetInventory->quantity;
            $newTargetQty = $oldTargetQty + $validated['quantity'];

            // 設定活動紀錄原因
            $targetInventory->activityLogReason = "撥補入庫 來自 {$sourceWarehouse->name}";
            $targetInventory->update(['quantity' => $newTargetQty]);

            // 記錄目標異動
            $targetInventory->transactions()->create([
                'type' => '撥補入庫',
                'quantity' => $validated['quantity'],
                'balance_before' => $oldTargetQty,
                'balance_after' => $newTargetQty,
                'reason' => "來自 {$sourceWarehouse->name} 的撥補" . ($validated['notes'] ? " ({$validated['notes']})" : ""),
                'actual_time' => $validated['transferDate'],
                'user_id' => auth()->id(),
            ]);

            // TODO: 未來若有獨立的 TransferOrder 模型，可在此建立紀錄

            return redirect()->back()->with('success', '撥補單已建立且庫存已轉移');
        });
    }

    /**
     * 獲取特定倉庫的庫存列表 (API)
     */
    public function getWarehouseInventories(Warehouse $warehouse)
    {
        $inventories = $warehouse->inventories()
            ->with(['product.baseUnit', 'product.category'])
            ->where('quantity', '>', 0) // 只回傳有庫存的
            ->get()
            ->map(function ($inv) {
                return [
                    'productId' => (string) $inv->product_id,
                    'productName' => $inv->product->name,
                    'batchNumber' => 'BATCH-' . $inv->id, // 模擬批號
                    'availableQty' => (float) $inv->quantity,
                    'unit' => $inv->product->baseUnit?->name ?? '個',
                ];
            });

        return response()->json($inventories);
    }
}
