<?php

use App\Modules\Procurement\Controllers\GoodsReceiptBulkOperationsController;
use App\Modules\Procurement\Controllers\GoodsReceiptPageController;
use App\Modules\Procurement\Controllers\GoodsReceiptWorkflowController;
use App\Modules\Procurement\Controllers\PurchaseOrderBulkOperationsController;
use App\Modules\Procurement\Controllers\PurchaseOrderPageController;
use App\Modules\Procurement\Controllers\PurchaseOrderTransitionController;
use Illuminate\Support\Facades\Route;

Route::prefix('procurement/purchase-orders')->name('procurement.purchase-orders.')->group(function (): void {
    Route::get('/', [PurchaseOrderPageController::class, 'index'])
        ->middleware('can:procurement.purchase_orders.view')
        ->name('index');

    Route::get('/{purchase_order}', [PurchaseOrderPageController::class, 'show'])
        ->middleware('can:procurement.purchase_orders.view')
        ->name('show');

    Route::post('/{purchase_order}/approve', [PurchaseOrderTransitionController::class, 'approve'])
        ->middleware('can:operate,purchase_order')
        ->name('approve.store');

    Route::post('/{purchase_order}/cancel', [PurchaseOrderTransitionController::class, 'cancel'])
        ->middleware('can:operate,purchase_order')
        ->name('cancel.store');

    Route::post('/{purchase_order}/goods-receipts', [GoodsReceiptWorkflowController::class, 'store'])
        ->middleware('can:createReceipt,purchase_order')
        ->name('goods-receipts.store');

    Route::post('/bulk/approve', [PurchaseOrderBulkOperationsController::class, 'approve'])
        ->middleware('can:procurement.purchase_orders.view')
        ->name('bulk.approve');

    Route::post('/bulk/cancel-drafts', [PurchaseOrderBulkOperationsController::class, 'cancelDrafts'])
        ->middleware('can:procurement.purchase_orders.view')
        ->name('bulk.cancel-drafts');
});

Route::prefix('procurement/goods-receipts')->name('procurement.goods-receipts.')->group(function (): void {
    Route::get('/', [GoodsReceiptPageController::class, 'index'])
        ->middleware('can:procurement.goods_receipts.view')
        ->name('index');

    Route::get('/{goods_receipt}', [GoodsReceiptPageController::class, 'show'])
        ->middleware('can:procurement.goods_receipts.view')
        ->name('show');

    Route::post('/{goods_receipt}/validate', [GoodsReceiptWorkflowController::class, 'submitValidation'])
        ->middleware('can:transition,goods_receipt')
        ->name('validate.store');

    Route::post('/{goods_receipt}/post', [GoodsReceiptWorkflowController::class, 'submitPosting'])
        ->middleware('can:transition,goods_receipt')
        ->name('post.store');

    Route::post('/bulk/validate', [GoodsReceiptBulkOperationsController::class, 'validateReceipts'])
        ->middleware('can:procurement.goods_receipts.view')
        ->name('bulk.validate');
});
