<?php

namespace App\Http\Controllers\Superadmin;

use App\Http\Controllers\Controller;
use App\Models\KabKota;
use App\Models\Kcd;
use Illuminate\Http\Request;

class KabKotaController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $kabKota = KabKota::with('kcd')->orderBy('name')->get();
        $kcds = Kcd::where('is_active', true)->orderBy('name')->get();
        return view('superadmin.kab-kota.index', compact('kabKota', 'kcds'));
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        return view('superadmin.kab-kota.create');
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $request->validate([
            'nama' => 'required|string|max:100|unique:kab_kota,name',
            'kcd_code' => 'nullable|string|exists:kcds,code',
        ], [
            'nama.required' => 'Nama Kab/Kota harus diisi',
            'nama.unique' => 'Nama Kab/Kota sudah terdaftar',
            'kcd_code.exists' => 'KCD tidak valid',
        ]);

        try {
            $kabKota = KabKota::create([
                'name' => $request->nama,
                'kcd_code' => $request->kcd_code,
                'is_active' => true,
            ]);

            return redirect()->route('superadmin.kab-kota.index')
                ->with('success', "Kab/Kota \"{$kabKota->name}\" berhasil ditambahkan");
        } catch (\Exception $e) {
            return redirect()->route('superadmin.kab-kota.index')
                ->with('error', 'Gagal menambahkan Kab/Kota: ' . $e->getMessage());
        }
    }

    /**
     * Display the specified resource.
     */
    public function show($id)
    {
        return view('superadmin.kab-kota.show');
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit($id)
    {
        return view('superadmin.kab-kota.edit');
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, $id)
    {
        $kabKota = KabKota::findOrFail($id);

        $request->validate([
            'nama' => 'required|string|max:100|unique:kab_kota,name,' . $id,
            'kcd_code' => 'nullable|string|exists:kcds,code',
        ], [
            'nama.required' => 'Nama Kab/Kota harus diisi',
            'nama.unique' => 'Nama Kab/Kota sudah terdaftar',
            'kcd_code.exists' => 'KCD tidak valid',
        ]);

        try {
            $kabKota->update([
                'name' => $request->nama,
                'kcd_code' => $request->kcd_code,
            ]);

            return redirect()->route('superadmin.kab-kota.index')
                ->with('success', "Kab/Kota \"{$kabKota->name}\" berhasil diperbarui");
        } catch (\Exception $e) {
            return redirect()->route('superadmin.kab-kota.index')
                ->with('error', 'Gagal memperbarui Kab/Kota: ' . $e->getMessage());
        }
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy($id)
    {
        try {
            $kabKota = KabKota::findOrFail($id);
            $kabKotaName = $kabKota->name;
            $kabKota->delete();

            return redirect()->route('superadmin.kab-kota.index')
                ->with('success', "Kab/Kota \"{$kabKotaName}\" berhasil dihapus");
        } catch (\Exception $e) {
            return redirect()->route('superadmin.kab-kota.index')
                ->with('error', 'Gagal menghapus Kab/Kota: ' . $e->getMessage());
        }
    }
}
