<?php

namespace App\Http\Controllers;

use App\Http\Requests\VerifikatorRequest;
use App\Models\Verifikator;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Inertia\Inertia;

class VerifikatorController extends Controller
{
    public function index()
    {
        try {
            $verifikator = Verifikator::latest()->get();
            return Inertia::render('admin/verifikator/index_verifikator', ['verifikator' => $verifikator]);
        } catch (\Exception $e) {
            Log::error('Error fetching Verifikator: ' . $e->getMessage());
            return back()->with('error', 'Failed to fetch verifikator.');
        }
    }

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

    public function update(VerifikatorRequest $request, Verifikator $verifikator)
    {
        try {
            $verifikator->update($request->validated());
            return redirect()->route('verifikator.index')->with('success', 'Verifikator updated successfully.');
        } catch (\Exception $e) {
            Log::error('Error updating verifikator: ' . $e->getMessage());
            return back()->with('error', 'Failed to update verifikator.');
        }
    }

    public function destroy(Verifikator $verifikator)
    {
        try {
            $verifikator->delete();
            return redirect()->route('verifikator.index')->with('success', 'Verifikator deleted successfully.');
        } catch (\Exception $e) {
            Log::error('Error deleting verifikator: ' . $e->getMessage());
            return back()->with('error', 'Failed to delete verifikator.');
        }
    }
}
