<?php

use App\Livewire\Auth\Register;
use App\Models\Invitation;
use App\Models\User;
use Livewire\Livewire;

uses(\Illuminate\Foundation\Testing\RefreshDatabase::class);

test('registration screen can be rendered', function () {
    $response = $this->get('/register');

    $response->assertStatus(200);
});

test('new users can register', function () {
    $response = Livewire::test(Register::class)
        ->set('name', 'Test User')
        ->set('email', 'test@example.com')
        ->set('password', 'password')
        ->set('password_confirmation', 'password')
        ->call('register');

    $response
        ->assertHasNoErrors()
        ->assertRedirect(route('dashboard', absolute: false));

    $this->assertAuthenticated();
});

test('registration without invitation code shows invitation required message', function () {
    $component = Livewire::test(Register::class);

    $component->assertSee('Invitation Required');
    $component->assertSee('An invitation is required to create an account');
    $component->assertSee('Registration is by invitation only');
    $component->assertSee('Return to login');
});

test('registration with valid invitation code shows registration form', function () {
    $invitation = Invitation::factory()->create([
        'email' => 'invited@example.com'
    ]);

    $component = Livewire::test(Register::class, ['code' => $invitation->code]);

    $component->assertSee('Create an account');
    $component->assertSee('Enter your details below to create your account');
    $component->assertSet('email', 'invited@example.com');
    $component->assertSet('invitation.email', 'invited@example.com');
});

test('user can register with valid invitation code', function () {
    $invitation = Invitation::factory()->create([
        'email' => 'invited@example.com'
    ]);

    $component = Livewire::test(Register::class, ['code' => $invitation->code])
        ->set('name', 'Invited User')
        ->set('email', 'invited@example.com')
        ->set('password', 'password123')
        ->set('password_confirmation', 'password123')
        ->call('register');

    $component
        ->assertHasNoErrors()
        ->assertRedirect(route('dashboard', absolute: false));

    $this->assertAuthenticated();

    // Verify invitation was accepted
    $invitation->refresh();
    expect($invitation->accepted_at)->not->toBeNull();
    expect($invitation->isPending())->toBeFalse();

    // Verify user was created with correct email
    $user = User::where('email', 'invited@example.com')->first();
    expect($user)->not->toBeNull();
    expect($user->name)->toBe('Invited User');
});

test('registration with non-existent invitation code shows invalid invitation message', function () {
    $component = Livewire::test(Register::class, ['code' => 'non-existent-code']);

    $component->assertSee('Invalid Invitation');
    $component->assertSee('This invitation link is not valid');
    $component->assertSee('The invitation link you used is invalid or has expired');
    $component->assertSee('Return to login');
    $component->assertSet('invitation', null);
});

test('registration with already accepted invitation shows invitation already used message', function () {
    $invitation = Invitation::factory()->accepted()->create([
        'email' => 'already-used@example.com'
    ]);

    $component = Livewire::test(Register::class, ['code' => $invitation->code]);

    $component->assertSee('Invitation Already Used');
    $component->assertSee('This invitation has already been accepted');
    $component->assertSee('This invitation code has already been used');
    $component->assertSee('Return to login');
    $component->assertSet('invitationAccepted', true);
});

test('cannot register with already accepted invitation', function () {
    $invitation = Invitation::factory()->accepted()->create([
        'email' => 'already-used@example.com'
    ]);

    $component = Livewire::test(Register::class, ['code' => $invitation->code])
        ->set('name', 'Test User')
        ->set('email', 'already-used@example.com')
        ->set('password', 'password123')
        ->set('password_confirmation', 'password123')
        ->call('register');

    $component->assertHasErrors(['general' => 'This invitation has already been accepted.']);
    $this->assertGuest();
});

test('email field is disabled when using invitation code', function () {
    $invitation = Invitation::factory()->create([
        'email' => 'invited@example.com'
    ]);

    $component = Livewire::test(Register::class, ['code' => $invitation->code]);

    // Check that email is pre-filled and component knows invitation exists
    $component->assertSet('email', 'invited@example.com');
    $component->assertSet('invitation.email', 'invited@example.com');
});

test('cannot register with different email than invitation email', function () {
    $invitation = Invitation::factory()->create([
        'email' => 'invited@example.com'
    ]);

    $component = Livewire::test(Register::class, ['code' => $invitation->code])
        ->set('name', 'Test User')
        ->set('email', 'different@example.com')
        ->set('password', 'password123')
        ->set('password_confirmation', 'password123')
        ->call('register');

    $component->assertHasErrors(['email']);
    $this->assertGuest();
});

test('registration requires all required fields', function () {
    $invitation = Invitation::factory()->create([
        'email' => 'invited@example.com'
    ]);

    $component = Livewire::test(Register::class, ['code' => $invitation->code])
        ->set('name', '')
        ->set('email', 'invited@example.com')
        ->set('password', '')
        ->set('password_confirmation', '')
        ->call('register');

    $component->assertHasErrors(['name', 'password']);
    $this->assertGuest();
});

test('registration requires password confirmation', function () {
    $invitation = Invitation::factory()->create([
        'email' => 'invited@example.com'
    ]);

    $component = Livewire::test(Register::class, ['code' => $invitation->code])
        ->set('name', 'Test User')
        ->set('email', 'invited@example.com')
        ->set('password', 'password123')
        ->set('password_confirmation', 'different-password')
        ->call('register');

    $component->assertHasErrors(['password']);
    $this->assertGuest();
});

test('registration without invitation requires unique email', function () {
    // Create an existing user
    User::factory()->create(['email' => 'existing@example.com']);

    $component = Livewire::test(Register::class)
        ->set('name', 'Test User')
        ->set('email', 'existing@example.com')
        ->set('password', 'password123')
        ->set('password_confirmation', 'password123')
        ->call('register');

    $component->assertHasErrors(['email']);
    $this->assertGuest();
});
