<?php

namespace App\Http\Controllers\Admin;

use App\FiveM\Contracts\FrameworkAdapter;
use App\Http\Controllers\Controller;
use App\Models\AuditLog;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;

class ItemManagerController extends Controller
{
    public function index(): Response
    {
        // Item catalogs (names/labels/weights) are defined in each
        // framework's own resource (e.g. qb-core/shared/items.lua) rather
        // than mirrored into a database table by default, so this page is
        // a grant tool keyed by the item's internal name string rather
        // than a full catalog browser.
        return Inertia::render('Admin/Items/Index');
    }

    public function grant(Request $request, FrameworkAdapter $adapter): RedirectResponse
    {
        $data = $request->validate([
            'citizen_id' => ['required', 'string', 'max:100'],
            'item_name' => ['required', 'string', 'max:100', 'alpha_dash'],
            'amount' => ['required', 'integer', 'min:1', 'max:10000'],
        ]);

        $adapter->giveItem($data['citizen_id'], $data['item_name'], $data['amount']);

        AuditLog::record('item.granted', null, $data);

        return back()->with('success', "Granted {$data['amount']}x {$data['item_name']}.");
    }
}
