<?php

namespace App\Http\Controllers\MasterData;

use App\Http\Controllers\Controller;
use App\Models\Land;
use App\Models\Province;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class LandController extends Controller
{
    public function index()
    {
        $lands = Land::orderBy('created_at', 'desc')->get();
        $provinces = Province::all();
        return view('master-data.lahan.index', compact('lands', 'provinces'));
    }

    public function store(Request $request)
    {
        $customMessage = [
            'owner.required' => 'Nama pemilik wajib diisi',
            'owner.max' => 'Nama pemilik maksimal 25 karakter',
            'owner.string' => 'Nama pemilik harus berupa string',

            'nohp.required' => 'Nomor HP wajib diisi',
            'nohp.min' => 'Nomor HP minimal 10 karakter',
            'nohp.max' => 'Nomor HP maksimal 13 karakter',
            'nohp.string' => 'Nomor HP harus berupa string',

            'rental_price.required' => 'Harga sewa wajib diisi',
            'rental_price.numeric' => 'Harga sewa harus berupa angka',

            'rental_start.required' => 'Tanggal mulai sewa wajib diisi',
            'rental_start.date' => 'Tanggal mulai sewa harus berupa tanggal',
            'rental_start.after_or_equal' => 'Tanggal mulai sewa harus hari ini atau setelahnya',

            'rental_until.required' => 'Tanggal sampai sewa wajib diisi',
            'rental_until.date' => 'Tanggal sampai sewa harus berupa tanggal',
            'rental_until.after_or_equal' => 'Tanggal sampai sewa harus sama atau lebih besar dari tanggal mulai sewa',

            'land_name.required' => 'Nama lahan wajib diisi',
            'land_name.max' => 'Nama lahan maksimal 25 karakter',
            'land_name.string' => 'Nama lahan harus berupa string',

            'province_id.required' => 'Provinsi wajib diisi',
            'province_id.exists' => 'Provinsi tidak ditemukan',

            'regency_id.required' => 'Kota wajib diisi',
            'regency_id.exists' => 'Kota tidak ditemukan',

            'district_id.required' => 'Kecamatan wajib diisi',
            'district_id.exists' => 'Kecamatan tidak ditemukan',

            'lat.required' => 'Latitude wajib diisi',
            'lat.numeric' => 'Latitude harus berupa angka',

            'lng.required' => 'Longitude wajib diisi',
            'lng.numeric' => 'Longitude harus berupa angka',
        ];

        $request->merge([
            'rental_price' => str_replace('.', '', $request->rental_price),
        ]);

        $validator = Validator::make($request->all(), [
            'owner' => 'required|string|max:25',
            'nohp' => 'required|string|min:10|max:13',
            'rental_price' => 'required|numeric',
            'rental_start' => 'required|date|after_or_equal:today',
            'rental_until' => 'required|date|after_or_equal:rental_start',
            'land_name' => 'required|string|max:25',
            'province_id' => 'required|exists:provinces,id',
            'regency_id' => 'required|exists:regencies,id',
            'district_id' => 'required|exists:districts,id',
            'lat' => 'required|numeric',
            'lng' => 'required|numeric',
        ], $customMessage);

        if ($validator->fails()) {
            toast($validator->messages()->all()[0], 'error')->position('top')->autoclose(3000);
            return redirect()->back()->withInput();
        }

        $land = new Land();
        $land->owner = $request->owner;
        $land->no_hp = $request->nohp;
        $land->rental_price = $request->rental_price;
        $land->rental_start = $request->rental_start;
        $land->rental_until = $request->rental_until;
        $land->land_name = $request->land_name;
        $land->province_code = $request->province_id;
        $land->regency_code = $request->regency_id;
        $land->district_code = $request->district_id;
        $land->latitude = $request->lat;
        $land->longitude = $request->lng;

        try {
            $land->save();
            toast('Data berhasil disimpan', 'success')->position('top-right')->autoclose(3000);
            return redirect()->back();
        } catch (\Throwable $th) {
            dd($th->getMessage());
            toast('Terjadi kesalahan', 'error')->position('top')->autoclose(3000);
            return redirect()->back();
        }
    }

    public function update(Request $request, $id)
    {
        $customMessage = [
            'owner.required' => 'Nama pemilik wajib diisi',
            'owner.max' => 'Nama pemilik maksimal 25 karakter',
            'owner.string' => 'Nama pemilik harus berupa string',

            'nohp.required' => 'Nomor HP wajib diisi',
            'nohp.min' => 'Nomor HP minimal 10 karakter',
            'nohp.max' => 'Nomor HP maksimal 13 karakter',
            'nohp.string' => 'Nomor HP harus berupa string',

            'rental_price.required' => 'Harga sewa wajib diisi',
            'rental_price.numeric' => 'Harga sewa harus berupa angka',

            'rental_start.required' => 'Tanggal mulai sewa wajib diisi',
            'rental_start.date' => 'Tanggal mulai sewa harus berupa tanggal',
            'rental_start.after_or_equal' => 'Tanggal mulai sewa harus hari ini atau setelahnya',

            'rental_until.required' => 'Tanggal sampai sewa wajib diisi',
            'rental_until.date' => 'Tanggal sampai sewa harus berupa tanggal',
            'rental_until.after_or_equal' => 'Tanggal sampai sewa harus sama atau lebih besar dari tanggal mulai sewa',

            'land_name.required' => 'Nama lahan wajib diisi',
            'land_name.max' => 'Nama lahan maksimal 25 karakter',
            'land_name.string' => 'Nama lahan harus berupa string',

            'province_id.required' => 'Provinsi wajib diisi',
            'province_id.exists' => 'Provinsi tidak ditemukan',

            'regency_id.required' => 'Kota wajib diisi',
            'regency_id.exists' => 'Kota tidak ditemukan',

            'district_id.required' => 'Kecamatan wajib diisi',
            'district_id.exists' => 'Kecamatan tidak ditemukan',

            'lat.required' => 'Latitude wajib diisi',
            'lat.numeric' => 'Latitude harus berupa angka',

            'lng.required' => 'Longitude wajib diisi',
            'lng.numeric' => 'Longitude harus berupa angka',
        ];

        $request->merge([
            'rental_price' => str_replace('.', '', $request->rental_price),
        ]);

        $validator = Validator::make($request->all(), [
            'owner' => 'required|string|max:25',
            'nohp' => 'required|string|min:10|max:13',
            'rental_price' => 'required|numeric',
            'rental_start' => 'required|date|after_or_equal:today',
            'rental_until' => 'required|date|after_or_equal:rental_start',
            'land_name' => 'required|string|max:25',
            'province_id' => 'required|exists:provinces,id',
            'regency_id' => 'required|exists:regencies,id',
            'district_id' => 'required|exists:districts,id',
            'lat' => 'required|numeric',
            'lng' => 'required|numeric',
        ], $customMessage);

        if ($validator->fails()) {
            toast($validator->messages()->all()[0], 'error')->position('top')->autoclose(3000);
            return redirect()->back()->withInput();
        }

        $land = Land::find($id);
        $land->land_name = $request->land_name;
        $land->owner = $request->owner;
        $land->no_hp = $request->nohp;
        $land->rental_price = $request->rental_price;
        $land->rental_start = $request->rental_start;
        $land->rental_until = $request->rental_until;
        $land->province_code = $request->province_id;
        $land->regency_code = $request->regency_id;
        $land->district_code = $request->district_id;
        $land->latitude = $request->lat;
        $land->longitude = $request->lng;

        try {
            $land->save();
            toast('Data berhasil diubah', 'success')->position('top-right')->autoclose(3000);
            return redirect()->back();
        } catch (\Throwable $th) {
            toast('Terjadi kesalahan', 'error')->position('top')->autoclose(3000);
            return redirect()->back();
        }
    }

    public function destroy($id)
    {
        $land = Land::find($id);
        try {
            $land->delete();
            toast('Data berhasil dihapus', 'success')->position('top-right')->autoclose(3000);
            return redirect()->back();
        } catch (\Throwable $th) {
            toast('Terjadi kesalahan', 'error')->position('top')->autoclose(3000);
            return redirect()->back();
        }
    }
}
