<?php

namespace App\Modules\Inventory\Controllers;

use App\Http\Controllers\Controller;

use App\Modules\Inventory\Models\Warehouse;
use App\Modules\Inventory\Models\WarehouseProductSafetyStock;
use App\Modules\Inventory\Models\Product;
use App\Modules\Inventory\Models\Inventory;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Illuminate\Support\Facades\DB;

class SafetyStockController extends Controller
{
    /**
     * 顯示安全庫存設定頁面
     */
    public function index(Warehouse $warehouse)
    {
        $allProducts = Product::with(['category', 'baseUnit'])->get();

        // 準備可選商品列表
        $availableProducts = $allProducts->map(function ($product) {
            return [
                'id' => (string) $product->id,
                'name' => $product->name,
                'type' => $product->category ? $product->category->name : '其他',
                'unit' => $product->baseUnit?->name ?? '個',
            ];
        });

        // 獲取現有庫存 (用於抓取「已在倉庫中」的商品)
        $inventoryProductIds = Inventory::where('warehouse_id', $warehouse->id)->pluck('product_id')->unique();

        // 準備安全庫存設定列表 (從資料庫讀取)
        $existingSettings = WarehouseProductSafetyStock::where('warehouse_id', $warehouse->id)
            ->with(['product.category', 'product.baseUnit'])
            ->get();

        $existingProductIds = $existingSettings->pluck('product_id')->toArray();

        // 找出：有庫存但是「還沒設定過安全庫存」的商品
        $missingProductIds = $inventoryProductIds->diff($existingProductIds);
        
        $missingProducts = Product::whereIn('id', $missingProductIds)
            ->with(['category', 'baseUnit'])
            ->get();

        // 合併：已設定的 + 有庫存未設定的 (預設值 0)
        $safetyStockSettings = $existingSettings->map(function ($setting) {
            return [
                'id' => (string) $setting->id,
                'warehouseId' => (string) $setting->warehouse_id,
                'productId' => (string) $setting->product_id,
                'productName' => $setting->product->name,
                'productType' => $setting->product->category ? $setting->product->category->name : '其他',
                'safetyStock' => (float) $setting->safety_stock,
                'unit' => $setting->product->baseUnit?->name ?? '個',
                'updatedAt' => $setting->updated_at->toIso8601String(),
                'isNew' => false, // 標記為舊有設定
            ];
        })->concat($missingProducts->map(function ($product) use ($warehouse) {
            return [
                'id' => 'temp_' . $product->id, // 暫時 ID
                'warehouseId' => (string) $warehouse->id,
                'productId' => (string) $product->id,
                'productName' => $product->name,
                'productType' => $product->category ? $product->category->name : '其他',
                'safetyStock' => 0, // 預設 0
                'unit' => $product->baseUnit?->name ?? '個',
                'updatedAt' => now()->toIso8601String(),
                'isNew' => true, // 標記為建議新增
            ];
        }))->values();

        // 原本的 inventories 映射 (供顯示對比)
        $inventories = Inventory::where('warehouse_id', $warehouse->id)
            ->select('product_id', DB::raw('SUM(quantity) as total_quantity'))
            ->groupBy('product_id')
            ->get()
            ->map(function ($inv) {
                return [
                    'productId' => (string) $inv->product_id,
                    'quantity' => (float) $inv->total_quantity,
                ];
            });

        return Inertia::render('Warehouse/SafetyStockSettings', [
            'warehouse' => $warehouse,
            'safetyStockSettings' => $safetyStockSettings,
            'inventories' => $inventories,
            'availableProducts' => $availableProducts,
        ]);
    }

    /**
     * 批量儲存安全庫存設定
     */
    public function store(Request $request, Warehouse $warehouse)
    {
        $validated = $request->validate([
            'settings' => 'required|array|min:1',
            'settings.*.productId' => 'required|exists:products,id',
            'settings.*.quantity' => 'required|numeric|min:0',
        ]);

        DB::transaction(function () use ($validated, $warehouse) {
            foreach ($validated['settings'] as $item) {
                WarehouseProductSafetyStock::updateOrCreate(
                    [
                        'warehouse_id' => $warehouse->id,
                        'product_id' => $item['productId'],
                    ],
                    [
                        'safety_stock' => $item['quantity'],
                    ]
                );
            }
        });

        return redirect()->back()->with('success', '安全庫存設定已更新');
    }

    /**
     * 更新單筆安全庫存設定
     */
    public function update(Request $request, Warehouse $warehouse, WarehouseProductSafetyStock $safetyStock)
    {
        $validated = $request->validate([
            'safetyStock' => 'required|numeric|min:0',
        ]);

        $safetyStock->update([
            'safety_stock' => $validated['safetyStock'],
        ]);

        return redirect()->back()->with('success', '安全庫存已更新');
    }

    /**
     * 刪除安全庫存設定
     */
    public function destroy(Warehouse $warehouse, WarehouseProductSafetyStock $safetyStock)
    {
        $safetyStock->delete();

        return redirect()->back()->with('success', '安全庫存設定已移除');
    }
}
