<?php

namespace Modules\Lpj\Http\Requests;

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

class SurveyorRequest extends FormRequest
{
    /**
     * Table mapping for different actionszz
     */
    private const TABLE_MAPPING = [
        'bentuk-tanah' => 'bentuk_tanah',
        'kontur-tanah' => 'kontur_tanah',
        'posisi-kavling' => 'posisi_kavling',
        'ketinggian-tanah' => 'ketinggian_tanah',
        'kondisi-fisik-tanah' => 'kondisi_fisik_tanah',
        'kondisi-bangunan' => 'kondisi_bangunan',
        'jenis-bangunan' => 'jenis_bangunan',
        'sifat-bangunan' => 'sifat_bangunan',
        'sarana-pelengkap' => 'sarana_pelengkap',
        'lalu-lintas-lokasi' => 'lalu_lintas_lokasi',
        'tingkat-keramaian' => 'tingkat_keramaian',
        'gol-mas-sekitar' => 'gol_mas_sekitar',
        'spek-kategori-bangunan' => 'spek_kategori_bangunan',
        'spek-bangunan' => 'spek_bangunan',
        'lantai-unit' => 'lantai',
        'view-unit' => 'view_unit',
        'jenis-pesawat' => 'jenis_pesawat',
        'model-alat-berat' => 'model_alat_berat',
        'jenis-kapal' => 'jenis_kapal',
        'jenis-kendaraan' => 'jenis_kendaraan',
        'jenis-unit' => 'jenis_unit',
        'terletak-area' => 'terletak_area',
        'merupakan-daerah' => 'merupakan_daerah',
        'posisi-unit' => 'posisi_unit',
        'bentuk-unit' => 'bentuk_unit',
        'fasilitas-objek' => 'fasilitas_objek',
        'perkerasan-jalan' => 'perkerasan_jalan',
        'foto-objek-jaminan' => 'foto_objek_jaminan',
        'perizinan' => 'perizinan'
    ];

    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     */
    public function rules(): array
    {
        return array_merge(
            $this->getBaseRules(),
            $this->getActionSpecificRules(),
            $this->getCodeValidationRules()
        );
    }

    /**
     * Get base validation rules
     */
    private function getBaseRules(): array
    {
        return [
            'name' => 'required|max:255',
        ];
    }

    /**
     * Get action specific validation rules
     */
    private function getActionSpecificRules(): array
    {
        $action = $this->input('action');

        return match ($action) {
            'spek-bangunan' => [
                'spek_kategori_bangunan_id' => [
                    'required',

                ],
            ],
            'foto-objek-jaminan' => [
                'kategori' => 'required',
            ],
            // Add more action specific rules here
            default => [],
        };
    }

    /**
     * Get code validation rules
     */
    private function getCodeValidationRules(): array
    {
        $action = $this->input('action');
        $table = self::TABLE_MAPPING[$action] ?? null;

        if (!$table) {
            return [];
        }

        $rules = ['required', 'max:50'];

        if ($this->isMethod('PUT') || $this->isMethod('PATCH')) {
            $rules[] = Rule::unique($table, 'code')->ignore($this->route('id'));
        } else {
            $rules[] = Rule::unique($table, 'code');
        }

        return ['code' => $rules];
    }

    /**
     * Prepare the data for validation.
     */
    protected function prepareForValidation(): void
    {
        $this->merge([
            'status' => true,
        ]);
    }

    /**
     * Get custom messages for validator errors.
     */
    public function messages(): array
    {
        return [
            'name.required' => 'Nama harus diisi',
            'name.max' => 'Nama tidak boleh lebih dari 255 karakter',
            'code.required' => 'Kode harus diisi',
            'code.max' => 'Kode tidak boleh lebih dari 50 karakter',
            'code.unique' => 'Kode sudah digunakan',
            'spek_kategori_bangunan_id.required' => 'Kategori bangunan harus dipilih',
        ];
    }

    /**
     * Get custom attributes for validator errors.
     */
    public function attributes(): array
    {
        return [
            'name' => 'Nama',
            'code' => 'Kode',
            'spek_kategori_bangunan_id' => 'Kategori Bangunan',
        ];
    }
}
