<?php

namespace App\Filament\Resources\MainCategoryResource\Pages;

use App\Filament\Resources\MainCategoryResource;
use App\Models\Catalog\MainCategory;
use App\Services\AuditLog;
use Filament\Resources\Pages\CreateRecord;

class CreateMainCategory extends CreateRecord
{
    protected static string $resource = MainCategoryResource::class;

    protected function mutateFormDataBeforeCreate(array $data): array
    {
        $data['sort_code'] = MainCategoryResource::generateSortCode($data['name']);

        return $data;
    }

    protected function afterCreate(): void
    {
        /** @var MainCategory $category */
        $category = $this->record;

        AuditLog::record('main_category.created', $category, [
            'name' => $category->name,
            'description' => 'Created main category "'.$category->name.'"'.($category->is_active ? '' : ' (inactive)').'.',
        ]);
    }

    protected function getRedirectUrl(): string
    {
        return MainCategoryResource::getUrl('index');
    }
}
