<?php

namespace App\Http\Requests\Wilayah;

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

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

    public function rules(): array
    {
        $tingkat = $this->route('tingkat');
        $table = "wilayah_{$tingkat}";

        return [
            'kode' => [
                'sometimes',
                'required',
                'string',
                'max:20',
                Rule::unique($table, 'kode')->ignore($this->route('wilayah')),
            ],
            'nama' => ['sometimes', 'required', 'string', 'max:255'],
            'provinsi_id' => [
                Rule::excludeIf($tingkat !== 'kabupaten'),
                'sometimes',
                'required',
                'integer',
                'exists:wilayah_provinsi,id',
            ],
            'kabupaten_id' => [
                Rule::excludeIf($tingkat !== 'kecamatan'),
                'sometimes',
                'required',
                'integer',
                'exists:wilayah_kabupaten,id',
            ],
            'kecamatan_id' => [
                Rule::excludeIf($tingkat !== 'desa'),
                'sometimes',
                'required',
                'integer',
                'exists:wilayah_kecamatan,id',
            ],
        ];
    }
}
