<?php

namespace App\Http\Controllers\Admin;

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

class BanController extends Controller
{
    public function index(): Response
    {
        return Inertia::render('Admin/Bans/Index', [
            'bans' => Ban::query()->latest()->paginate(25),
        ]);
    }

    public function store(StoreBanRequest $request): RedirectResponse
    {
        $ban = Ban::create([
            ...$request->validated(),
            'banned_by' => $request->user()->name,
        ]);

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

        return back()->with('success', 'Player banned.');
    }

    public function destroy(Ban $ban): RedirectResponse
    {
        AuditLog::record('ban.lifted', $ban, $ban->only(['citizenid', 'license']));
        $ban->delete();

        return back()->with('success', 'Ban lifted.');
    }
}
