<?php

namespace App\Modules\Procurement\Controllers;

use App\Http\Controllers\Controller;
use App\Modules\Procurement\Requests\BulkApprovePurchaseOrdersRequest;
use App\Modules\Procurement\Requests\BulkCancelDraftPurchaseOrdersRequest;
use App\Modules\Procurement\Services\PurchaseOrderBulkApproveService;
use App\Modules\Procurement\Services\PurchaseOrderBulkCancelDraftsService;
use Illuminate\Http\RedirectResponse;

class PurchaseOrderBulkOperationsController extends Controller
{
    public function approve(
        BulkApprovePurchaseOrdersRequest $request,
        PurchaseOrderBulkApproveService $service,
    ): RedirectResponse {
        $user = $request->user();
        $tenantId = (int) $user->tenant_id;
        abort_if($tenantId === 0, 403);
        $result = $service->run($user, $tenantId, $request->validated('ids'));

        return back()->with('bulk_result', $result->toSessionPayload());
    }

    public function cancelDrafts(
        BulkCancelDraftPurchaseOrdersRequest $request,
        PurchaseOrderBulkCancelDraftsService $service,
    ): RedirectResponse {
        $user = $request->user();
        $tenantId = (int) $user->tenant_id;
        abort_if($tenantId === 0, 403);
        $result = $service->run($user, $tenantId, $request->validated('ids'));

        return back()->with('bulk_result', $result->toSessionPayload());
    }
}
