<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\JsonResponse;

class DiscordRoleSyncController extends Controller
{
    /**
     * Pulled by the FiveM resource (e.g. on player connect, or a periodic
     * export call) so in-game permissions can reflect the UCP's
     * Discord-synced roles without waiting for the player to next log into
     * the dashboard.
     */
    public function __invoke(string $discordId): JsonResponse
    {
        $user = User::query()->where('discord_id', $discordId)->first();

        if (! $user) {
            return response()->json(['linked' => false, 'roles' => []]);
        }

        return response()->json([
            'linked' => true,
            'roles' => $user->getRoleNames(),
            'fivem_license' => $user->fivem_license,
        ]);
    }
}
