<?php

namespace App\Http\Controllers;

use App\Enums\AccessRequestStatus;
use App\Http\Requests\AccessRequestsRequest;
use App\Models\AccessRequests;
use App\Models\GameServers;
use Illuminate\Http\JsonResponse;

class AccessRequestsController extends Controller
{
    public function store(AccessRequestsRequest $request): JsonResponse
    {
        $validated = $request->validated();
        $serverId = $validated['server_id']
            ?? GameServers::where('slug', $validated['server_slug'])->valueOrFail('id');

        $accessRequest = AccessRequests::create([
            'game_username' => $validated['game_username'],
            'message' => $validated['message'] ?? '',
            'server_id' => $serverId,
            'status' => AccessRequestStatus::PENDING,
        ]);

        return response()->json($accessRequest, 201);
    }
}
