<?php

namespace App\Modules\Inventory\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Inertia\Inertia;
use App\Modules\Inventory\Services\TraceabilityService;

class TraceabilityController extends Controller
{
    public function __construct(
        protected TraceabilityService $traceabilityService
    ) {}

    /**
     * 顯示批號溯源查詢的主頁面
     */
    public function index(Request $request)
    {
        $batchNumber = $request->input('batch_number');
        $direction = $request->input('direction', 'backward'); // backward 或 forward

        $result = null;

        if ($batchNumber) {
            if ($direction === 'backward') {
                $result = $this->traceabilityService->traceBackward($batchNumber);
            } else {
                $result = $this->traceabilityService->traceForward($batchNumber);
            }
        }

        return Inertia::render('Inventory/Traceability/Index', [
            'search' => [
                'batch_number' => $batchNumber,
                'direction' => $direction,
            ],
            'result' => $result
        ]);
    }
}
