<?php

namespace App\Modules\Procurement\Controllers;

use App\Http\Controllers\Controller;
use App\Modules\Procurement\Models\ShippingOrder;
use App\Modules\Inventory\Contracts\InventoryServiceInterface;
use App\Modules\Core\Contracts\CoreServiceInterface;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Illuminate\Support\Facades\DB;

class ShippingOrderController extends Controller
{
    protected $inventoryService;
    protected $coreService;
    protected $shippingService;

    public function __construct(
        InventoryServiceInterface $inventoryService, 
        CoreServiceInterface $coreService,
        \App\Modules\Procurement\Services\ShippingService $shippingService
    ) {
        $this->inventoryService = $inventoryService;
        $this->coreService = $coreService;
        $this->shippingService = $shippingService;
    }

    public function index(Request $request)
    {
        return Inertia::render('Common/UnderConstruction', [
            'featureName' => '出貨單管理'
        ]);

        /* 原有邏輯暫存
        $query = ShippingOrder::query();

        // 搜尋
        if ($request->search) {
            $query->where(function($q) use ($request) {
                $q->where('doc_no', 'like', "%{$request->search}%")
                  ->orWhere('customer_name', 'like', "%{$request->search}%");
            });
        }

        // 狀態篩選
        if ($request->status && $request->status !== 'all') {
            $query->where('status', $request->status);
        }

        $perPage = $request->input('per_page', 10);
        $orders = $query->orderBy('id', 'desc')->paginate($perPage)->withQueryString();

        // 水和倉庫與使用者
        $warehouses = $this->inventoryService->getAllWarehouses();
        $userIds = $orders->getCollection()->pluck('created_by')->filter()->unique()->toArray();
        $users = $this->coreService->getUsersByIds($userIds)->keyBy('id');

        $orders->getCollection()->transform(function ($order) use ($warehouses, $users) {
            $order->warehouse_name = $warehouses->firstWhere('id', $order->warehouse_id)?->name ?? 'Unknown';
            $order->creator_name = $users->get($order->created_by)?->name ?? 'System';
            return $order;
        });

        return Inertia::render('ShippingOrder/Index', [
            'orders' => $orders,
            'filters' => $request->only(['search', 'status', 'per_page']),
            'warehouses' => $warehouses->map(fn($w) => ['id' => $w->id, 'name' => $w->name]),
        ]);
        */
    }

    public function create()
    {
        return Inertia::render('Common/UnderConstruction', [
            'featureName' => '出貨單建立'
        ]);

        /* 原有邏輯暫存
        $warehouses = $this->inventoryService->getAllWarehouses();
        $products = $this->inventoryService->getAllProducts();
        
        return Inertia::render('ShippingOrder/Create', [
            'warehouses' => $warehouses->map(fn($w) => ['id' => $w->id, 'name' => $w->name]),
            'products' => $products->map(fn($p) => [
                'id' => $p->id,
                'name' => $p->name,
                'code' => $p->code,
                'unit_name' => $p->baseUnit?->name,
            ]),
        ]);
        */
    }

    public function store(Request $request)
    {
        return back()->with('error', '出貨單管理功能正在製作中');
    }

    public function show($id)
    {
        return Inertia::render('Common/UnderConstruction', [
            'featureName' => '出貨單詳情'
        ]);
    }

    public function edit($id)
    {
        return Inertia::render('Common/UnderConstruction', [
            'featureName' => '出貨單編輯'
        ]);
    }

    public function update(Request $request, $id)
    {
        return back()->with('error', '出貨單管理功能正在製作中');
    }

    public function post($id)
    {
        return back()->with('error', '出貨單管理功能正在製作中');
    }

    public function destroy($id)
    {
        $order = ShippingOrder::findOrFail($id);
        if ($order->status !== 'draft') {
            return back()->withErrors(['error' => '僅能刪除草稿狀態的單據']);
        }
        $order->delete();
        return redirect()->route('delivery-notes.index')->with('success', '出貨單已刪除');
    }
}
