<?php

namespace App\Modules\Inventory\Controllers;

use App\Http\Controllers\Controller;
use App\Modules\Inventory\DTOs\ItemCategoryWriteData;
use App\Modules\Inventory\Models\ItemCategory;
use App\Modules\Inventory\Requests\StoreItemCategoryRequest;
use App\Modules\Inventory\Services\ItemCategoryManagementService;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;

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

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

        return view('pages.inventory.categories.create', [
            'parentOptions' => $this->management->parentSelectOptions(),
        ]);
    }

    public function store(StoreItemCategoryRequest $request): RedirectResponse
    {
        $this->management->store(ItemCategoryWriteData::fromValidated($request->validated()));

        return redirect()
            ->route('inventory.categories.index')
            ->with('status', __('inventory.categories.flash.created'));
    }
}
