<?php

use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

it('returns available true for valid free username', function () {
    $response = $this->getJson('/api/username/availability?username=free_name');

    $response->assertOk()->assertJson([
        'available' => true,
        'normalized' => 'free_name',
    ]);
});

it('returns validation error for invalid username format', function () {
    $response = $this->getJson('/api/username/availability?username=bad.name');

    $response->assertStatus(422)->assertJsonPath('available', false);
});

it('returns validation error for reserved username', function () {
    $response = $this->getJson('/api/username/availability?username=admin');

    $response->assertStatus(422)->assertJsonPath('available', false);
});

it('returns available false for already taken username', function () {
    User::factory()->create(['username' => 'taken_name']);

    $response = $this->getJson('/api/username/availability?username=taken_name');

    $response->assertOk()->assertJson([
        'available' => false,
        'normalized' => 'taken_name',
    ]);
});
