<?php

namespace App\Modules\Procurement\Controllers;

use App\Http\Controllers\Controller;
use App\Modules\Procurement\Models\GoodsReceipt;
use App\Support\Operational\GoodsReceiptOperationalPresenter;
use Illuminate\Support\Str;
use Illuminate\View\View;

class GoodsReceiptPageController extends Controller
{
    public function index(): View
    {
        $this->authorize('viewAny', GoodsReceipt::class);

        return view('pages.procurement.goods-receipts.index', [
            'receipts' => GoodsReceipt::query()
                ->with(['purchaseOrder:id,po_number', 'warehouse:id,code,name'])
                ->orderByDesc('id')
                ->paginate(25),
        ]);
    }

    public function show(GoodsReceipt $goodsReceipt): View
    {
        $this->authorize('view', $goodsReceipt);
        $goodsReceipt->load([
            'purchaseOrder.items.inventoryItem',
            'warehouse:id,code,name',
            'items.purchaseOrderItem.inventoryItem',
        ]);

        $operational = app(GoodsReceiptOperationalPresenter::class)->detail($goodsReceipt);

        return view('pages.procurement.goods-receipts.show', [
            'receipt' => $goodsReceipt,
            'postIdempotencyKey' => (string) Str::uuid(),
            ...$operational,
        ]);
    }
}
