<?php

namespace Modules\Lpj\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class FormSurveyorRequest extends FormRequest
{
    /**
     * 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
    {

        $actionSpecificRules = $this->getActionSpecificRules();
        return $actionSpecificRules;
    }

    /**
     * Get rules specific to the action.
     */
    private function getActionSpecificRules(): array
    {
        $action = $this->input('action');
        $pisah = explode(',', $action);

        $allRules = [
            'tanah' => $this->getTanahRules(),
            'bangunan' => $this->getBangunanRules(),
            'kapal' => $this->getKapalRules(),
            'kendaraan' => $this->getKendaraanRules(),
            'mesin' => $this->getMesinRules(),
            'pesawat' => $this->getPesawatRules(),
            'alat-berat' => $this->getAlatBeratRules(),
            'apartemen-kantor' => $this->getUnitRules(),
            'lingkungan' => $this->getLinkunganRules(),
            'fakta' => $this->getCommonRules(),
            'rap' => $this->getRapRules()
        ];

        $rules = [];
        $hasAssetDescriptionRules = false;

        foreach ($pisah as $act) {
            if (isset($allRules[$act])) {
                $rules = array_merge($rules, $allRules[$act]);
                if ($act == 'tanah' || $act == 'bangunan' || $act == 'apartemen-kantor' || $act == 'rap') {
                    $hasAssetDescriptionRules = true;
                }
            }
        }

        if ($hasAssetDescriptionRules) {
            $rules = array_merge($rules, $this->getAssetDescriptionRules());
        }

        return $rules;
    }
    /**
     * Get rules specific to tanah action.
     */

    public function getTanahRules(): array
    {
        return  [
            'luas_tanah' => 'required',
            'luas_tanah_sesuai' => 'nullable',
            'luas_tanah_tidak_sesuai' => 'nullable',
            'hadap_mata_angin' => 'required',
            'hadap_mata_angin_sesuai' => 'nullable',
            'hadap_mata_angin_tidak_sesuai' => 'nullable',
            'bentuk_tanah' => 'required|array',
            'bentuk_tanah_lainnya' => 'nullable',
            'kontur_tanah' => 'required|array',
            'ketinggian_jalan' => 'required|array',
            'kontur_jalan' => 'required',
            'posisi_kavling' => 'required|array',
            'posisi_kavling_lainnya' => 'nullable',
            'tusuk_sate' => 'required',
            'lockland' => 'required',
            'kondisi_fisik_tanah' => 'required|array',
            'ketinggian_lebih_tinggi' => 'nullable',
            'ketinggian_lebih_rendah' => 'nullable',
        ];
    }

    /**
     * Get rules specific to Bangunan action.
     */

    private function getBangunanRules(): array
    {
        return  [
            'action' => 'required',
            'luas_tanah_bangunan_sesuai' => 'nullable',
            'luas_tanah_bagunan' => 'required',
            'luas_tanah_bangunan_tidak_sesuai' => 'nullable',
            'jenis_bangunan' => 'required|array',
            'kondisi_bangunan' => 'required|array',
            'sifat_bangunan' => 'required|array',
            'sifat_bangunan_input' => 'nullable|array',


            'nama_bangunan.*' => 'required|string|max:255',
            'spek_kategori_bangunan.*' => 'nullable|string',
            'spek_bangunan.*.*.lainnya' => 'nullable|string',

            'sarana_pelengkap' => 'required',
            'sarana_pelengkap_input' => 'nullable|array',
        ];
    }

    /**
    * Get rules specific to unit action.
    */
    private function getUnitRules(): array
    {
        return  [
            'action' => 'required',
            'luas_unit' => 'required',
            'luas_unit_sesuai' => 'nullable',
            'luas_unit_tidak_sesuai' => 'nullable',
            'kondisi_unit' => 'required|array',
            'posisi_unit' => 'required|array',
            'lantai' => 'required|array',
            'view' => 'required|array',
            'bentuk_unit' => 'required|array',
        ];
    }

    /**
     * Get rules specific to Linkungan action.
     */

    private function getLinkunganRules(): array
    {
        return [
            'action' => 'required',
            'jarak_jalan_utama' => 'nullable',
            'jalan_linkungan' => 'nullable',
            'jarak_cbd_point' => 'nullable',
            'nama_cbd_point' => 'nullable',
            'lebar_perkerasan_jalan' => 'nullable',
            'perkerasan_jalan' => 'nullable|array',
            'perkerasan_jalan_lainnya' => 'nullable',
            'lalu_lintas' => 'nullable',
            'gol_mas_sekitar' => 'nullable',
            'tingkat_keramaian' => 'nullable',
            'terletak_diarea' => 'nullable',
            'terletak_diarea_lainnya' => 'nullable',
            'disekitar_lokasi' => 'required',
            'kondisi_bagunan_disekitar_lokasi' => 'nullable',
            'sifat_bagunan_disekitar_lokasi' => 'nullable',
            'dekat_makam' => 'nullable',
            'jarak_makam' => 'nullable',
            'nama_makam' => 'nullable',
            'dekat_tps' => 'nullable',
            'jarak_tps' => 'nullable',
            'nama_tpu' => 'nullable',
            'dekat_lainnya' => 'nullable',
            'merupakan_daerah' => 'nullable',
            'fasilitas_dekat_object' => 'nullable|array',
            'fasilitas_dekat_object_input' => 'nullable|array',
        ];
    }

    private function getKapalRules(): array
    {
        return [
            'action' => 'required',
            'nama_wakil_debitur' => 'nullable',
            'hub_calon_debitur' => 'required',
            'dermaga' => 'nullable',
            'nama_jalan' => 'required',
            'perumahan_gang' => 'required',
            'blok_nomor' => 'required',

            'village_code' => 'nullable|string',
            'district_code' => 'nullable|string',
            'city_code' => 'nullable|string',
            'province_code' => 'nullable|string',

            'jenis_kapal' => 'required',
            'jenis_kapal_lainnya' => 'nullable',
            'size' => 'required',
            'kondisi' => 'required',
            'klasifikasi' => 'required',

            'nama_kapal' => 'required',
            'pemilik_kapal' => 'required',
            'bendera' => 'required',
            'nomor_selar' => 'required',
            'kapal' => 'required',
            'galangan_kapal' => 'required',
            'kapal_shipyard' => 'required',
            'tahun_pembuatan' => 'required',
            'tahun_launcing' => 'required',
            'dwt' => 'required',
            'lwt' => 'required',
            'gross_tonnage' => 'required',
            'net_tonnage' => 'required',
            'tenaga_mesin' => 'required',
            'loa' => 'required',
            'lbp' => 'required',
            'beam' => 'required',
            'depth' => 'required',
            'draft' => 'required',

            'lambung_kapal' => 'required',
            'dek' => 'required',
            'struktur_rangka' => 'required',
            'palka' => 'required',
            'pondasi_mesin' => 'required',
            'area_mesin' => 'required',
            'cat_dan_korosi' => 'required',
            'sistem_pengelasan' => 'required',
            'deskripsi_struktur' => 'required',

            'sekoci' => 'required',
            'jaket_pelampung' => 'required',
            'alat_pemadaman' => 'required',
            'rambu_darurat' => 'required',
            'sistem_alarm' => 'required',
            'sistem_pencegah' => 'required',
            'kebakaran' => 'required',
            'lampu_darurat' => 'required',
            'deskripsi_peralatan' => 'required',

            'gps' => 'required',
            'radar' => 'required',
            'radio_komunikasi' => 'required',
            'lampu_navigasi' => 'required',
            'sistem_kendali_otomatis' => 'required',
            'kompas' => 'required',
            'deskripsi_navigasi' => 'required',

            'mesin_utama' => 'required',
            'mesin_bantu' => 'required',
            'pompa_pendingin' => 'required',
            'sistem_pelumasan' => 'required',
            'propeller' => 'required',
            'sistem_kelistrikan' => 'required',
            'deskripsi_mesin_penggerak' => 'required',

            'lampu_navigasi' => 'required',
            'sistem_penerangan' => 'required',
            'sistem_panel_distribusi' => 'required',
            'kabel_perangkat' => 'required',
            'deskripsi_kelistrikan' => 'required',

            'kebersihan_dek_luar' => 'nullable',
            'tangki_limbah' => 'nullable',
            'sistem_pengelolaan_limbah' => 'nullable',
            'pengelolaan_air_ballast' => 'nullable',
            'deskripsi_kebersihan' => 'required',

            'fakta_positif.*' => 'nullable',
            'fakta_negatif.*' => 'nullable',
            'analisa_makro.*' => 'nullable',
            'kesimpulan.*' => 'nullable',
            'catatan.*' => 'nullable',
        ];
    }

    public function getKendaraanRules(): array
    {
        return [
            'action' => 'required',
            'tanggal_survey' => 'required',
            'nama_wakil' => 'required',
            'hub_calon_debitur' => 'required',
            'nama_jalan' => 'required',
            'perumahan_gang' => 'required',
            'blok_nomor' => 'required',

            'village_code' => 'nullable|string',
            'district_code' => 'nullable|string',
            'city_code' => 'nullable|string',
            'province_code' => 'nullable|string',

            'masa_stnk' => 'required',
            'masa_pajak' => 'required',
            'kendaraan.*' => 'required',
            'kendaraan_input.*' => 'nullable',
            'kondisi' => 'required',
            'nomor_polisi' => 'required',
            'nomor_polis_tidak_sesuai' => 'nullable',
            'merek' => 'required',
            'merek_tidak_sesuai' => 'nullable',
            'warna' => 'required',
            'warna_tidak_sesuai' => 'nullable',
            'nomor_rangka' => 'required',
            'nomor_rangka_tidak_sesuai' => 'nullable',
            'nomor_mesin' => 'required',
            'nomor_mesin_tidak_sesuai' => 'nullable',
            'posisi_kilometer' => 'required',
            'transmisi' => 'required',
            'transmisi_input' => 'nullable',
            'mesin_panel_instrument.*' => 'required',
            'mesin_panel_instrument_input.*' => 'nullable',
            'fungsi_mesin_panel_instrument.*' => 'required',
            'fungsi_mesin_panel_instrument_input.*' => 'nullable',
            'interior.*' => 'required',
            'interior_input.*' => 'nullable',
            'jumlah_pintu.*' => 'required',
            'jumlah_pintu_input.*' => 'nullable',
            'rangka_karoseri.*' => 'required',
            'rangka_karoseri_input.*' => 'nullable',
            'ban.*' => 'required',
            'ban_input.*' => 'nullable',
            'velg.*' => 'required',
            'velg_input.*' => 'nullable',
            'bamper_depan.*' => 'required',
            'bamper_depan_input.*' => 'nullable',
            'bamper_belakang.*' => 'required',
            'bamper_belakang_input.*' => 'nullable',
            'lampu_depan.*' => 'required',
            'lampu_depan_input.*' => 'nullable',
            'lampu_belakang.*' => 'required',
            'lampu_belakang_input.*' => 'nullable',
            'kaca_kendaraan.*' => 'required',
            'kaca_kendaraan_input.*' => 'nullable',
            'air_conditioner.*' => 'required',
            'air_conditioner_input.*' => 'nullable',
            'tape_radio_cd.*' => 'required',
            'tape_radio_cd_input.*' => 'nullable',
            'sensor_parkir.*' => 'required',
            'sensor_parkir_input.*' => 'nullable',
            'sensor_camera_recorder.*' => 'required',
            'sensor_camera_recorder_input.*' => 'nullable',
            'lcd.*' => 'required',
            'lcd_input.*' => 'nullable',
            'sabuk_keselamatan.*' => 'required',
            'sabuk_keselamatan_input.*' => 'nullable',
            'airbag.*' => 'required',
            'airbag_input.*' => 'nullable',
            'asuransi.*' => 'required',
            'asuransi_input.*' => 'nullable',
            'perusahaan_asuransi' => 'required',
            'tahun_berakhir' => 'required',
            'fakta_positif.*' => 'nullable',
            'fakta_negatif.*' => 'nullable',
            'analisa_makro.*' => 'nullable',
            'kesimpulan.*' => 'nullable',
            'catatan.*' => 'nullable',
        ];
    }


    public function getMesinRules(): array
    {
        return [
            'action' => 'required',
            'nama_wakil' => 'required',
            'nama_jalan' => 'required',
            'perumahan_gang' => 'required',
            'blok' => 'required',
            'desa_kelurahan' => 'required',
            'kecamatan' => 'required',
            'kota_madya' => 'required',
            'provinsi' => 'required',
            'hub_calon_debitur' => 'required',
           'tipe_model' => 'required',
           'merek' => 'required',
           'tahun_pembuatan' => 'required',
           'negara_pembuat' => 'required',
           'kondisi_mesin' => 'required',
           'faktor_positif' => 'nullable',
            'faktor_negatif' => 'nullable',
            'kesimpulan' => 'nullable',
            'catatan' => 'nullable',
        ];
    }


    public function getAlatBeratRules(): array
    {
        return [
            'action' => 'required',
            'nama_wakil' => 'required|string',
            'hub_calon_debitur' => 'required|string',
            'nama_jalan' => 'required',
            'perumahan_gang' => 'required',
            'blok_nomor' => 'required',
            'village_code' => 'nullable|string',
            'district_code' => 'nullable|string',
            'city_code' => 'nullable|string',
            'province_code' => 'nullable|string',
            'jenis_model' => 'required',
            'nomor_lambung' => 'required',
            'model_unit' => 'required',
            'tahun_pembuatan' => 'required',
            'merk' => 'required',
            'negara_pembuat' => 'required',
            'tahun_pembelian' => 'required',
             'nomor_faktur' => 'nullable',
             'nomor_kontrak' => 'nullable',
             'nama_pemilik' => 'nullable',
             'alamat_pemilik' => 'nullable',
             'nomor_asuransi' => 'nullable',
             'nomor_rangka' => 'nullable',
             'nomor_mesin' => 'nullable',
             'hour_mesters' => 'nullable',
             'overhaul_mesin' => 'nullable',

             'mesin_panel.*' => 'nullable',
             'mesin_panel_input.*' => 'nullable',
             'fungsi_panel.*' => 'nullable',
             'fungsi_panel_input.*' => 'nullable',
             'interior.*' => 'nullable',
             'interior_input.*' => 'nullable',
             'rangka_Karoseri.*' => 'nullable',
             'rangka_Karoseri_input.*' => 'nullable',
             'ban.*' => 'nullable',
             'ban_innput.*' => 'nullable',
             'velg.*' => 'nullable',
             'velg_input.*' => 'nullable',
             'air_conditioner.*' => 'nullable',
             'air_conditioner_input.*' => 'nullable',
             'aksesoris.*' => 'nullable',
             'aksesoris_input.*' => 'nullable',
             'lcd.*' => 'nullable',
             'lcd_innput.*' => 'nullable',
             'perlengkapan.*' => 'nullable',
             'perlengkapan_input.*' => 'nullable',
             'asuransi.*' => 'nullable',
             'asuransi_input.*' => 'nullable',
             'perusahaan_asuransi' => 'nullable',
             'tahun_berakhir.*' => 'nullable',
             'sensor_kamera.*' => 'nullable',
             'lcd.*' => 'nullable',
             'sabuk_keselamatan.*' => 'nullable',
             'air_bag.*' => 'nullable',
             'asuransi.*' => 'nullable',
             'perusahan_asuransi' => 'nullable',
             'tahun_berakhir' => 'nullable',
            'fakta_positif' => 'nullable|array',
            'fakta_negatif' => 'nullable|array',
            'kesimpulan' => 'nullable',
            'catatan' => 'nullable|array',
        ];
    }



    private function getPesawatRules(): array
    {
        return [
            'action' => 'required',
            'nama_wakil' => 'required|string',
            'hub_calon_debitur' => 'required|string',
            'nama_jalan' => 'required',
            'perumahan_gang' => 'required',
            'blok_nomor' => 'required',

            'village_code' => 'nullable|string',
            'district_code' => 'nullable|string',
            'city_code' => 'nullable|string',
            'province_code' => 'nullable|string',

            'jenis_pesawat' => 'required',
            'jenis_pesawat_lainnya' => 'nullable',
            'size' => 'required',
            'kondisi' => 'required',
            'nama_pesawat' => 'required',
            'model' => 'required',
            'nomor_registrasi' => 'required',
            'tahun_pembuatan' => 'required',
            'certificate_of_airworthines' => 'required',
            'certificate_of_registration' => 'required',
            'total_service_hours' => 'required',
            'total_service_cycles' => 'required',

            'last_a_check' => 'required',
            'next_a_check' => 'required',
            'last_b_check' => 'required',
            'next_b_check' => 'required',
            'last_c_check' => 'required',
            'next_c_check' => 'required',
            'next_d_check' => 'required',
            'last_d_check' => 'required',
            'deskripsi_maintenence' => 'nullable',

            'instrument_landing_system' => 'required',
            'traffic_collision_avoidance_system' => 'required',
            'windshear' => 'required',
            'electronic_flight' => 'required',
            'winglets' => 'required',
            'deskripsi_konfigurasi' => 'required',


            'maksimal_penumpang' => 'required',
            'jumlah_kursi' => 'required',
            'kursi_pramugari_pramugara' => 'required',
            'kartu_fitur_keselamatan' => 'required',
            'sabuk_pengaman' => 'required',
            'lampu_kabin' => 'required',
            'lampu_pintu_keluar' => 'required',
            'intercom_kabin' => 'required',
            'deskripsi_kabin' => 'required',


            'badan_pesawat' => 'nullable',
            'sayap_pesawat' => 'required',
            'ekor_pesawat' => 'required',
            'landing_gear' => 'required',
            'sabuk_pengaman' => 'required',
            'sistem_pengelasan' => 'required',
            'deskripsi_struktur' => 'required',

            'gps' => 'required',
            'radar' => 'required',
            'radio_komunikasi' => 'required',
            'lampu_navigasi' => 'required',
            'sistem_autopilot' => 'required',
            'deskripsi_navigasi' => 'required',

            'tangki_bahan_bakar' => 'required',
            'saluran_pipa_bahan_bakar' => 'required',
            'pompa_bahan_bakar' => 'required',
            'sistem_hidrolik_utama' => 'required',
            'sistem_pendigin_hidrolik' => 'required',
            'deskripsi_hidrolik' => 'required',

            'mesin_utama' => 'required',
            'sistem_pendorong' => 'required',
            'sistem_pendigin_mesin' => 'required',
            'sistem_pelumasan' => 'required',
            'filter_dan_perangkat_pendukung' => 'required',
            'deskripsi_kondisi_mesin' => 'required',


            'jaket_pelampung' => 'required',
            'pintu_darurat' => 'required',
            'alat_pemadaman_kebakaran' => 'required',
            'sistem_alaram_darurat' => 'required',
            'sekoci' => 'required',
            'masker_oxigen' => 'required',
            'sabuk_pengaman' => 'required',
            'deskripsi_fungsi_keselamatan' => 'required',


            'sistem_ventilasi_ac' => 'required',
            'sistem_penerangan_kabin' => 'required',
            'panel_informasi_penumpang' => 'required',
            'sistem_hiburan_kabin' => 'required',
            'deskripsi_Interior' => 'required',

            'fakta_positif' => 'nullable|array',
            'fakta_negatif' => 'nullable|array',
            'kesimpulan' => 'nullable',
            'catatan' => 'nullable|array',


        ];
    }

    private function getAssetDescriptionRules(): array
    {
        return [
            'permohonan_id' => 'required',
            'dokument_id' => 'required',
            'type' => 'required',
            'nomor_registrasi' => 'required',
            'debitur_perwakilan' => 'required|array',
            'jenis_asset_name' => 'nullable|',
            'jenis_asset' => 'required',
            'jenis_asset_tidak_sesuai' => 'nullable|string',
            'alamat_sesuai' => 'required',
            'alamat_tidak_sesuai' => 'nullable|string',
            'pihak_bank' => 'nullable|string',
            'nomor_nib' => 'nullable|string',
            'hub_cadeb' => 'nullable|string',
            'hub_cadeb_sesuai' => 'nullable|string',
            'hub_cadeb_tidak_sesuai' => 'nullable|string',
            'hub_cadeb_penghuni' => 'nullable',
            'hub_cadeb_penghuni_sesuai' => 'nullable|string',
            'hub_penghuni_tidak_sesuai' => 'nullable|string',

            'address' => 'nullable|string',
            'village_code' => 'nullable|string',
            'district_code' => 'nullable|string',
            'city_code' => 'nullable|string',
            'province_code' => 'nullable|string',
            'kordinat_lng' => 'nullable|string',
            'kordinat_lat' => 'nullable|string',
        ];
    }



    /**
     * Get common rules that apply to all actions.
     */
    private function getCommonRules(): array
    {
        return [
            'fakta_positif' => 'nullable|array',
            'fakta_negatif' => 'nullable|array',
            'rute_menuju' => 'nullable',
            'batas_batas' => 'required|array',
            'batas_batas_input' => 'nullable|array',
            'kondisi_lingkungan' => 'nullable|array',
            'kondisi_lain_bangunan' => 'nullable|array',
            'informasi_dokument' => 'nullable|array',
            'peruntukan' => 'nullable',
            'kdb' => 'nullable',
            'kdh' => 'nullable',
            'gsb' => 'nullable',
            'max_lantai' => 'nullable',
            'klb' => 'nullable',
            'gss' => 'nullable',
            'pelebaran_jalan' => 'nullable',
            'nama_petugas' => 'nullable',
            'lat' => 'nullable|numeric',
            'lng' => 'nullable|numeric',
            'foto_gistaru' => 'nullable',
            'foto_bhumi' => 'nullable',
            'foto_argis_region' => 'nullable',
            'foto_tempat' => 'nullable',
            'upload_gs' => 'nullable',
            'foto_sentuh_tanahku' => 'nullable',
            'keterangan' => 'nullable|array',
        ];
    }

    private function getRapRules()
    {
        return [


            'perizinan' => 'nullable|array',
            'perizinan.*' => 'nullable|string',
            'perizinan_file' => 'nullable|array',
            'perizinan_file.*' => 'nullable|file|mimes:pdf,docx',

            'brosur_price_list' => 'nullable|array',
            'brosur_price_list.*' => 'nullable|string',
            'brosur_price_list_file' => 'nullable|array',
            'brosur_price_list_file.*' => 'nullable|file|mimes:pdf,docx',

            'pengalaman_developer' => 'nullable',
            'developer_anggota' => 'nullable',
            'lainnya_developer.*' => 'nullable',
            'kapan_mulai_dibangun' => 'nullable',
            'kondisi_perumahan' => 'nullable',
            'progres_pembangunan' => 'nullable',
            'kontraktor' => 'nullable',
            'lingkungan_sekitar' => 'nullable',
            'komplek_disekitar' => 'nullable',
            'pusat_keramaian' => 'nullable',
            'transportasi_umum' => 'nullable',
            'lainnya_kondisi.*' => 'nullable',

             // Validasi untuk partisi yang diperbarui
        'partisi' => 'nullable|array',
        'partisi.*' => 'nullable|array',
        'partisi.*.nama' => 'nullable|string',
        'partisi.*.value' => 'nullable|string|max:255',

            'jumlah_unit.*' => 'nullable',
            'batas_batas_perumahan' => 'nullable',
            'fasus_fasum.*' => 'nullable',
            'progres_penjualan.*' => 'nullable',
            'harga_unit.*' => 'nullable',
            'target_market.*' => 'nullable',
            'kerjasama_dengan_bank' => 'nullable',
            'rute_menuju_lokasi' => 'nullable',
            'peruntukan' => 'nullable',
            'kdb' => 'nullable',
            'kdh' => 'nullable',
            'gsb' => 'nullable',
            'max_lantai' => 'nullable',
            'klb' => 'nullable',
            'gss' => 'nullable',
            'pelebaran_jalan' => 'nullable',
            'nama_petugas' => 'nullable',
            'lat' => 'nullable|numeric',
            'lng' => 'nullable|numeric',
            'foto_gistaru' => 'nullable',
            'foto_bhumi' => 'nullable',
            'foto_argis_region' => 'nullable',
            'foto_tempat' => 'nullable',
            'upload_gs' => 'nullable',
            'foto_sentuh_tanahku' => 'nullable',
            'keterangan' => 'nullable|array',

        ];
    }

}
