<?php

namespace App\Http\Controllers\Admin;

use App\FiveM\Models\Warning;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\StoreWarningRequest;
use App\Models\AuditLog;
use Illuminate\Http\RedirectResponse;
use Inertia\Inertia;
use Inertia\Response;

class WarnController extends Controller
{
    public function index(): Response
    {
        return Inertia::render('Admin/Warnings/Index', [
            'warnings' => Warning::query()->latest('created_at')->paginate(25),
        ]);
    }

    public function store(StoreWarningRequest $request): RedirectResponse
    {
        $warning = Warning::create([
            ...$request->validated(),
            'warned_by' => $request->user()->name,
        ]);

        AuditLog::record('warning.created', $warning, $warning->only(['citizenid', 'reason']));

        return back()->with('success', 'Warning issued.');
    }

    public function destroy(Warning $warning): RedirectResponse
    {
        AuditLog::record('warning.deleted', $warning, $warning->only(['citizenid']));
        $warning->delete();

        return back()->with('success', 'Warning removed.');
    }
}
