<?php

namespace App\Http\Controllers\Website;

use App\Http\Controllers\Controller;
use App\Models\HeightVillage;
use App\Models\PhVillage;
use App\Models\PreferenceResultVillage;
use App\Models\SubDistrict;
use App\Models\Village;
use App\Models\VillageAlternatif;
use App\Models\WeatherVillage;
use Illuminate\Http\Request;

class ListVillageController extends Controller
{
    public function index()
    {
        $listSubDistrict = SubDistrict::all();
        return view('website.app.list-village', compact('listSubDistrict'));
    }

    public function getListVillage($subDistrictId)
    {
        $listVillage = Village::select(
            'village.id',
            'village.sub_district_id',
            'village.village',
            'village.latitude',
            'village.longitude',
            'sub_district.sub_district'
        )
            ->join('sub_district', 'village.sub_district_id', '=', 'sub_district.id')
            ->where('village.sub_district_id', '=', $subDistrictId)
            ->get();

        return response()->json([
            'listVillage' => $listVillage
        ]);
    }

    public function addVillage(Request $request)
    {
        $request->validate([
            'village' => 'required|string|max:100|unique:village,village',
        ]);

        $subDistrictId = $request->input('subDistrictId');
        $village = $request->input('village');
        $latitude = number_format((float) $request->input('latitude'), 5, '.', '');
        $longitude = number_format((float) $request->input('longitude'), 5, '.', '');

        Village::create([
            'sub_district_id' => $subDistrictId,
            'village' => $village,
            'latitude' => $latitude,
            'longitude' => $longitude
        ]);

        toast('Data desa atau kelurahan berhasil ditambahkan!', 'success');
        return redirect()->back();
    }

    public function updateVillage(Request $request)
    {
        $villageById = Village::find($request->input('villageId'));

        $villageById['village'] = $request->input('village');
        $villageById['latitude'] = number_format((float) $request->input('latitude'), 5, '.', '');
        $villageById['longitude'] = number_format((float) $request->input('longitude'), 5, '.', '');

        $villageById->save();
        toast('Data desa / kelurahan berhasil diubah', 'success');
        return redirect()->back();
    }

    public function deleteVillage($id)
    {
        $villageById = Village::find($id);

        // hapus pada relasi tabel ketinggian tempat
        $heightVillage = HeightVillage::where('village_id', '=', $id)->get();
        foreach ($heightVillage as $value) {
            $value->delete();
        }

        // hapus pada relasi tabel cuaca
        $weatherVillage = WeatherVillage::where('village_id', '=', $id)->get();
        foreach ($weatherVillage as $value) {
            $value->delete();
        }

        // hapus pada relasi tabel phH2O
        $phH2OVillage = PhVillage::where('village_id', '=', $id)->get();
        foreach ($phH2OVillage as $value) {
            $value->delete();
        }

        // hapus pada relasi tabel alternatif desa
        $alternatifVillage = VillageAlternatif::where('village_id', '=', $id)->get();
        foreach ($alternatifVillage as $value) {
            $value->delete();
        }

        // hapus pada relasi tabel preference desa
        $preferenceVillage = PreferenceResultVillage::where('village_id', '=', $id)->get();
        foreach ($preferenceVillage as $value) {
            $value->delete();
        }

        // hapus kecamatan
        $villageById->delete();

        toast('Data desa / kelurahan berhasil dihapus', 'success');
        return redirect()->back();
    }
}
