<?php

namespace App\Modules\Inventory\Controllers;

use App\Http\Controllers\Controller;
use App\Modules\Inventory\DTOs\WarehouseWriteData;
use App\Modules\Inventory\Models\Warehouse;
use App\Modules\Inventory\Requests\StoreWarehouseRequest;
use App\Modules\Inventory\Services\WarehouseManagementService;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;

final class WarehouseCreateController extends Controller
{
    public function __construct(
        private readonly WarehouseManagementService $management,
    ) {}

    public function create(): View
    {
        $this->authorize('create', Warehouse::class);

        return view('pages.inventory.warehouses.create');
    }

    public function store(StoreWarehouseRequest $request): RedirectResponse
    {
        $warehouse = $this->management->store(
            WarehouseWriteData::fromValidated($request->validated()),
        );

        return redirect()
            ->route('inventory.warehouses.show', $warehouse)
            ->with('status', __('inventory.warehouses.flash.created'));
    }
}
