<?php

namespace Tests\Feature;

use App\Enums\AccessRequestStatus;
use App\Models\GameServers;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class AccessRequestsApiTest extends TestCase
{
    use RefreshDatabase;

    public function test_access_request_can_be_created_from_api(): void
    {
        $server = GameServers::create([
            'name' => 'Survival',
            'slug' => 'survival',
        ]);

        $response = $this->postJson('/api/access-requests', [
            'game_username' => 'LeonPlayer',
            'message' => 'Je veux rejoindre le serveur.',
            'server_id' => $server->id,
        ]);

        $response
            ->assertCreated()
            ->assertJsonPath('game_username', 'LeonPlayer')
            ->assertJsonPath('message', 'Je veux rejoindre le serveur.')
            ->assertJsonPath('server_id', $server->id)
            ->assertJsonPath('status', AccessRequestStatus::PENDING->value);

        $this->assertDatabaseHas('access_requests', [
            'game_username' => 'LeonPlayer',
            'message' => 'Je veux rejoindre le serveur.',
            'server_id' => $server->id,
            'status' => AccessRequestStatus::PENDING->value,
        ]);
    }

    public function test_api_always_creates_pending_access_requests(): void
    {
        $server = GameServers::create([
            'name' => 'Survival',
            'slug' => 'survival',
        ]);

        $this->postJson('/api/access-requests', [
            'game_username' => 'LeonPlayer',
            'server_id' => $server->id,
            'status' => AccessRequestStatus::ACCEPTED->value,
        ])->assertCreated();

        $this->assertDatabaseHas('access_requests', [
            'game_username' => 'LeonPlayer',
            'server_id' => $server->id,
            'status' => AccessRequestStatus::PENDING->value,
        ]);
    }

    public function test_access_request_can_be_created_with_a_server_slug(): void
    {
        $server = GameServers::create([
            'name' => 'Survival',
            'slug' => 'survival',
        ]);

        $this->postJson('/api/access-requests', [
            'game_username' => 'LeonPlayer',
            'server_slug' => 'survival',
        ])->assertCreated();

        $this->assertDatabaseHas('access_requests', [
            'game_username' => 'LeonPlayer',
            'server_id' => $server->id,
            'status' => AccessRequestStatus::PENDING->value,
        ]);
    }
}
