<?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 EconomyController extends Controller
{
    public function index(FrameworkAdapter $adapter): Response
    {
        return Inertia::render('Admin/Economy/Index', [
            'topCash' => $adapter->topByMoney('cash', 10)->map->toArray(),
            'topBank' => $adapter->topByMoney('bank', 10)->map->toArray(),
        ]);
    }

    public function adjust(Request $request, FrameworkAdapter $adapter): RedirectResponse
    {
        $data = $request->validate([
            'citizen_id' => ['required', 'string', 'max:100'],
            'account' => ['required', 'in:cash,bank,crypto'],
            'delta' => ['required', 'integer'],
        ]);

        $adapter->adjustMoney($data['citizen_id'], $data['account'], $data['delta']);

        AuditLog::record('economy.adjusted', null, $data);

        return back()->with('success', 'Balance adjusted.');
    }
}
