<?php

namespace App\Http\Requests;

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

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

    public function rules(): array
    {
        return [
            'district_id' => ['required', 'exists:districts,id'],

            // exactly one required: upazila_id OR city_id
            'upazila_id' => ['nullable', 'exists:upazilas,id', 'required_without:city_id', 'prohibited_unless:city_id,null'],
            'city_id'    => ['nullable', 'exists:cities,id', 'required_without:upazila_id', 'prohibited_unless:upazila_id,null'],

            'ward_no' => ['required', 'integer', 'min:1', 'max:300'],

            'name_bn' => ['nullable', 'string', 'max:255'],
            'name_en' => ['nullable', 'string', 'max:255'],

            // unique within the selected parent
            Rule::unique('wards')->where(fn($q) => $q
                ->where('city_id', $this->input('city_id'))
                ->where('upazila_id', $this->input('upazila_id'))
            ),
        ];
    }

    public function messages(): array
    {
        return [
            'district_id.required' => 'জেলা নির্বাচন করুন',
            'upazila_id.required_without' => 'উপজেলা অথবা সিটি নির্বাচন করুন',
            'city_id.required_without' => 'উপজেলা অথবা সিটি নির্বাচন করুন',
            'ward_no.required' => 'ওয়ার্ড নম্বর দিন',
            'ward_no.integer' => 'ওয়ার্ড নম্বর সংখ্যা হতে হবে',
        ];
    }
}
