<?php

namespace Modules\Lpj\Services;

class TypeLaporanService
{
    public function handleRequest($type, $permohonanId, $documentId, $jaminanId, $surveyorController)
    {
        $permohonan = $this->getPermohonanData($surveyorController, $permohonanId, $documentId, $jaminanId);

        if ($permohonan->status == 'proses-laporan') {
            throw new \Exception('Masih dalam proses laporan');
        }

        $nomorLaporan = $this->generateNomorLaporan($surveyorController, $permohonan, $documentId, $type);
        $inspeksi = $this->getInspeksi($permohonanId, $documentId);
        $this->updatePenilai($permohonanId, $documentId, $type);

        $debitur = Debiture::find($permohonan->debiture_id);
        $forminspeksi = $inspeksi ? json_decode($inspeksi->data_form, true) : null;

        $locationCodes = $this->getAlamatData($debitur, $forminspeksi);
        $locationData = $this->getLocationData(
            $locationCodes['provinceCode'],
            $locationCodes['cityCode'],
            $locationCodes['districtCode'],
            $forminspeksi
        );

        return [
            'permohonan' => $permohonan,
            'nomorLaporan' => $nomorLaporan,
            'basicData' => $surveyorController->getCommonData(),
            'cities' => $locationData['cities'],
            'districts' => $locationData['districts'],
            'villages' => $locationData['villages'],
            'forminspeksi' => $forminspeksi,
        ];
    }

    private function getPermohonanData($surveyorController, $permohonanId, $documentId, $jaminanId)
    {
        return $surveyorController->getPermohonanData($permohonanId, $documentId, $jaminanId);
    }

    private function generateNomorLaporan($surveyorController, $permohonan, $documentId, $type)
    {
        return $surveyorController->generateNomorLaporan($permohonan, $documentId, $type);
    }

    private function getInspeksi($permohonanId, $documentId)
    {
        return Inspeksi::where('permohonan_id', $permohonanId)
            ->where('document_id', $documentId)
            ->first();
    }

    private function updatePenilai($permohonanId, $documentId, $type)
    {
        // Logika untuk memperbarui data berdasarkan tipe laporan
    }

    private function getAlamatData($debitur, $forminspeksi)
    {
        return [
            'provinceCode' => $debitur->province_id ?? $forminspeksi['province'],
            'cityCode' => $debitur->city_id ?? $forminspeksi['city'],
            'districtCode' => $debitur->district_id ?? $forminspeksi['district'],
        ];
    }

    private function getLocationData($provinceCode, $cityCode, $districtCode, $forminspeksi)
    {
        return [
            'cities' => $this->fetchCityData($provinceCode),
            'districts' => $this->fetchDistrictData($cityCode),
            'villages' => $this->fetchVillageData($districtCode),
        ];
    }

    private function fetchCityData($provinceCode)
    {
        return City::where('province_code', $provinceCode)->get();
    }
    private function fetchDistrictData($cityCode)
    {
        return District::where('city_code', $cityCode)->get();
    }
    private function fetchVillageData($districtCode)
    {
        return  Village::where('district_code', $districtCode)->get();
    }

    private function updateOrCreatePenilai($permohonan_id, $document_id, $type, $data)
    {
        return Penilai::updateOrCreate(
            [
                     'permohonan_id' => $permohonan_id,
                     'dokument_id' => $document_id,

                 ],
            [
                     $type => json_encode($data),
                 ]
        );
    }
}
