<?php

namespace App\Http\Controllers\Website;

use App\Http\Controllers\Controller;
use App\Models\HeightSubDistrict;
use App\Models\SubDistrict;
use App\Models\WeatherSubDistrict;
use App\Models\PhSubDistrict;
use App\Models\Village;
use App\Models\SubDistrictAlternatif;
use App\Models\PreferenceResultSubDistrict;
use Illuminate\Http\Request;

class ListSubDistrictController extends Controller
{
    public function index()
    {
        return view('website.app.list-sub-district');
    }

    public function getListSubDistrict()
    {
        $listSubDistricts = SubDistrict::all();

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

    public function addSubDistrict(Request $request)
    {

        $request->validate([
            'subDistrict' => 'required|string|max:100|unique:sub_district,sub_district',
        ]);

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

        SubDistrict::create([
            'sub_district' => $subDistrict,
            'latitude' => $latitude,
            'longitude' => $longitude
        ]);

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

    public function updateSubDistrict(Request $request)
    {
        $subDistrictById = SubDistrict::find($request->input('subDistrictId'));

        $subDistrictById['sub_district'] = $request->input('subDistrict');
        $subDistrictById['latitude'] = number_format((float) $request->input('latitude'), 5, '.', '');
        $subDistrictById['longitude'] = number_format((float) $request->input('longitude'), 5, '.', '');

        $subDistrictById->save();
        toast('Data kecamatan berhasil diubah', 'success');
        return redirect()->back();
    }

    public function deleteSubDistrict($id)
    {
        $subDistrictById = SubDistrict::find($id);

        // // hapus pada relasi tabel desa
        // $villageBySubDistrict = Village::where('sub_district_id', '=', $id)->get();
        // foreach ($villageBySubDistrict as $value) {
        //     $value->delete();
        // }

        $villageBySubDistrict = Village::where('sub_district_id', '=', $id)->get();
        if (count($villageBySubDistrict) == 0) {
            // hapus pada relasi tabel ketinggian tempat
            $heightSubDistrict = HeightSubDistrict::where('sub_district_id', '=', $id)->get();
            foreach ($heightSubDistrict as $value) {
                $value->delete();
            }

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

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

            // hapus pada relasi tabel alternatif kecamatan
            $alternatifSubDistrict = SubDistrictAlternatif::where('sub_district_id', '=', $id)->get();
            foreach ($alternatifSubDistrict as $value) {
                $value->delete();
            }

            // hapus pada relasi tabel preference kecamatan
            $preferenceSubDistrict = PreferenceResultSubDistrict::where('sub_district_id', '=', $id)->get();
            foreach ($preferenceSubDistrict as $value) {
                $value->delete();
            }

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

            toast('Data kecamatan berhasil dihapus', 'success');
            return redirect()->back();
        } else {
            toast('Data kecamatan masih memiliki list data desa / kelurahan', 'error');
            return redirect()->back();
        }
    }
}
