<?php

namespace App\Http\Controllers\Superadmin;

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

class KcdController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $kcds = Kcd::all();
        return view('superadmin.kcd.index', compact('kcds'));
    }

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

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $request->validate([
            'code' => 'required|string|max:10|unique:kcds,code',
            'name' => 'required|string|max:100',
            'address' => 'nullable|string|max:255',
            'phone' => 'nullable|string|max:20',
            'head_name' => 'nullable|string|max:100',
            'head_nip' => 'nullable|string|max:20',
        ], [
            'code.required' => 'Kode KCD harus diisi',
            'code.unique' => 'Kode KCD sudah terdaftar',
            'name.required' => 'Nama KCD harus diisi',
        ]);

        try {
            $kcd = Kcd::create([
                'code' => $request->code,
                'name' => $request->name,
                'address' => $request->address,
                'phone' => $request->phone,
                'head_name' => $request->head_name,
                'head_nip' => $request->head_nip,
                'is_active' => true,
            ]);

            return redirect()->route('superadmin.kcd.index')
                ->with('success', "KCD \"{$kcd->name}\" berhasil ditambahkan");
        } catch (\Exception $e) {
            return redirect()->route('superadmin.kcd.index')
                ->with('error', 'Gagal menambahkan KCD: ' . $e->getMessage());
        }
    }

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

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

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

        $request->validate([
            'code' => 'required|string|max:10|unique:kcds,code,' . $kcd->id,
            'name' => 'required|string|max:100',
            'address' => 'nullable|string|max:255',
            'phone' => 'nullable|string|max:20',
            'head_name' => 'nullable|string|max:100',
            'head_nip' => 'nullable|string|max:20',
        ], [
            'code.required' => 'Kode KCD harus diisi',
            'code.unique' => 'Kode KCD sudah terdaftar',
            'name.required' => 'Nama KCD harus diisi',
        ]);

        try {
            $kcd->update([
                'code' => $request->code,
                'name' => $request->name,
                'address' => $request->address,
                'phone' => $request->phone,
                'head_name' => $request->head_name,
                'head_nip' => $request->head_nip,
            ]);

            return redirect()->route('superadmin.kcd.index')
                ->with('success', "KCD \"{$kcd->name}\" berhasil diperbarui");
        } catch (\Exception $e) {
            return redirect()->route('superadmin.kcd.index')
                ->with('error', 'Gagal memperbarui KCD: ' . $e->getMessage());
        }
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy($id)
    {
        try {
            $kcd = Kcd::findOrFail($id);
            $kcdName = $kcd->name;
            $kcd->delete();
            
            return redirect()->route('superadmin.kcd.index')->with('success', "KCD \"{$kcdName}\" berhasil dihapus");
        } catch (\Exception $e) {
            return redirect()->route('superadmin.kcd.index')->with('error', 'Gagal menghapus KCD: ' . $e->getMessage());
        }
    }
}
