<?php

namespace App\Http\Controllers;

use App\Http\Requests\PenaatanRequest;
use App\Models\Penaatan;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Inertia\Inertia;

class PenaatanController extends Controller
{
    public function index()
    {
        try {
            $penaatan = Penaatan::latest()->get();
            return Inertia::render('admin/penaatan/index_penaatan', ['penaatan' => $penaatan]);
        } catch (\Exception $e) {
            Log::error('Error fetching Status Penaatan: ' . $e->getMessage());
            return back()->with('error', 'Something went wrong.');
        }
    }

    public function store(PenaatanRequest $request)
    {
        try {
            Penaatan::create($request->validated());
            return redirect()->route('admin.penataan.index')->with('success', 'Penaatan created successfully.');
        } catch (\Exception $e) {
            Log::error('Error creating Penaatan: ' . $e->getMessage());
            return back()->with('error', 'Failed to create Penaatan.');
        }
    }

    public function update(PenaatanRequest $request, Penaatan $penaatan)
    {
        try {
            $penaatan->update($request->validated());
            return redirect()->route('admin.penaatan.index')->with('success', 'Penaatan berhasil diperbarui.');
        } catch (\Exception $e) {
            Log::error('Error updating Penaatan: ' . $e->getMessage());
            return back()->with('error', 'Something went wrong.');
        }
    }

    public function destroy(Penaatan $penaatan)
    {
        try {
            $penaatan->delete();
            return redirect()->route('admin.penaatan.index')->with('success', 'Penaatan berhasil dihapus.');
        } catch (\Exception $e) {
            Log::error('Error deleting Penaatan: ' . $e->getMessage());
            return back()->with('error', 'Something went wrong.');
        }
    }
}
