<?php

namespace App\Http\Controllers\Api\V1\Website;

use App\Domain\Website\Models\JobCategory;
use Illuminate\Database\Eloquent\Model;

class CmsCategoryController extends CmsResourceController
{
    protected function model(): string
    {
        return JobCategory::class;
    }

    protected function rules(bool $creating): array
    {
        return [
            'name' => [$creating ? 'required' : 'sometimes', 'string', 'max:255'],
            'description' => ['nullable', 'string', 'max:2000'],
            'icon' => ['nullable', 'string', 'max:40'],
            'is_published' => ['sometimes', 'boolean'],
            'is_featured' => ['sometimes', 'boolean'],
            'sort_order' => ['sometimes', 'integer', 'min:0', 'max:9999'],
        ];
    }

    protected function prepare(array $data, ?Model $record = null, ?int $companyId = null): array
    {
        if ($record === null && isset($data['name'])) {
            $data['slug'] = $this->uniqueSlug($data['name'], (int) $companyId);
        }

        return $data;
    }

    protected function present(Model $record): array
    {
        return [
            'id' => $record->uuid,
            'name' => $record->name,
            'slug' => $record->slug,
            'description' => $record->description,
            'icon' => $record->icon,
            'is_published' => $record->is_published,
            'is_featured' => $record->is_featured,
            'sort_order' => $record->sort_order,
        ];
    }
}
