<?php

namespace App\Http\Requests\Admin;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class KategoriRequest extends FormRequest
{
    public function authorize(): bool { return true; }

    public function rules(): array
    {
        $id = $this->route('kategori') ?? $this->input('id');

        return [
            'name'       => ['required', 'string', 'max:100'],
            'slug'       => ['nullable', 'string', 'max:120', Rule::unique('categories', 'slug')->ignore($id)],
            'image'      => ['nullable', 'image', 'mimes:jpg,jpeg,png,webp', 'max:2048'],
            'is_active'  => ['boolean'],
            'sort_order' => ['integer', 'min:0', 'max:9999'],
        ];
    }

    public function messages(): array
    {
        return [
            'name.required' => 'Nama kategori wajib diisi.',
            'name.max'      => 'Nama maksimal 100 karakter.',
            'image.image'   => 'File harus berupa gambar.',
            'image.max'     => 'Ukuran gambar maksimal 2MB.',
        ];
    }
}
