<?php

namespace App\Http\Controllers\Api\V1;

use App\FiveM\Models\Ban;
use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;

class BanCheckController extends Controller
{
    /**
     * Called from Lua on player connect (e.g. inside an
     * AddEventHandler('playerConnecting', ...) deferral). Returns whether
     * the connecting license is banned so the resource can call
     * `deferrals.done(reason)` before the player loads in.
     */
    public function __invoke(string $license): JsonResponse
    {
        $ban = Ban::query()
            ->where('license', $license)
            ->where(fn ($q) => $q->whereNull('expires_at')->orWhere('expires_at', '>', now()))
            ->latest()
            ->first();

        return response()->json([
            'banned' => (bool) $ban,
            'reason' => $ban?->reason,
            'expires_at' => $ban?->expires_at,
        ]);
    }
}
