<?php

namespace App\Modules\Sales\Controllers;

use App\Http\Controllers\Controller;
use App\Modules\Sales\Requests\BulkAllocateSalesOrdersRequest;
use App\Modules\Sales\Requests\BulkFulfillSalesOrdersRequest;
use App\Modules\Sales\Services\SalesOrderBulkAllocateMaxService;
use App\Modules\Sales\Services\SalesOrderBulkFulfillMaxService;
use Illuminate\Http\RedirectResponse;

class SalesOrderBulkOperationsController extends Controller
{
    public function allocateMax(
        BulkAllocateSalesOrdersRequest $request,
        SalesOrderBulkAllocateMaxService $service,
    ): RedirectResponse {
        $user = $request->user();
        $tenantId = (int) $user->tenant_id;
        abort_if($tenantId === 0, 403);
        $result = $service->run($user, $tenantId, $request->validated('order_ids'));

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

    public function fulfillMax(
        BulkFulfillSalesOrdersRequest $request,
        SalesOrderBulkFulfillMaxService $service,
    ): RedirectResponse {
        $user = $request->user();
        $tenantId = (int) $user->tenant_id;
        abort_if($tenantId === 0, 403);
        $result = $service->run($user, $tenantId, $request->validated('order_ids'));

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