<?php

use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Mail;

uses(RefreshDatabase::class);

it('requires authentication to open setup email screen', function () {
    $this->get('/setup/email')
        ->assertRedirect('/login');
});

it('renders setup email screen for authenticated user', function () {
    $user = User::factory()->create([
        'onboarding_step' => null,
    ]);

    $this->actingAs($user)
        ->withSession(['username_login_upgrade' => true])
        ->get('/setup/email')
        ->assertOk()
        ->assertSee('Add Your Email')
        ->assertSee('Continue');
});

it('saves email during setup email step and moves to password setup when reset is required', function () {
    Mail::fake();

    $user = User::factory()->create([
        'email' => 'legacy-user@users.skinbase.org',
        'onboarding_step' => null,
        'needs_password_reset' => true,
    ]);

    $response = $this->actingAs($user)
        ->withSession(['username_login_upgrade' => true])
        ->post('/setup/email', [
            'email' => 'upgraded@example.com',
        ]);

    $response->assertRedirect('/setup/password');
    $response->assertSessionHas('status', 'Email saved. Continue with password setup.');

    $user->refresh();
    expect($user->email)->toBe('upgraded@example.com');
    expect($user->onboarding_step)->toBe('verified');
    expect($user->email_verified_at)->toBeNull();

    Mail::assertNothingQueued();
});

it('saves email during setup email step and moves to username step when password reset is not required', function () {
    Mail::fake();

    $user = User::factory()->create([
        'email' => 'legacy-user@users.skinbase.org',
        'email_verified_at' => null,
        'onboarding_step' => null,
        'needs_password_reset' => false,
    ]);

    $response = $this->actingAs($user)
        ->withSession(['username_login_upgrade' => true])
        ->post('/setup/email', [
            'email' => 'upgraded@example.com',
        ]);

    $response->assertRedirect('/setup/username');

    $user->refresh();
    expect($user->email)->toBe('upgraded@example.com');
    expect($user->onboarding_step)->toBe('password');
    expect($user->email_verified_at)->toBeNull();
    Mail::assertNothingQueued();
});

it('redirects username-login upgrade users to username step after email save', function () {
    $user = User::factory()->create([
        'onboarding_step' => 'password',
    ]);

    $this->actingAs($user)
        ->withSession(['username_login_upgrade' => true])
        ->get('/dashboard')
        ->assertRedirect('/setup/username');
});

it('redirects username-login upgrade users to password step when reset is still required', function () {
    $user = User::factory()->create([
        'onboarding_step' => 'verified',
    ]);

    $this->actingAs($user)
        ->withSession(['username_login_upgrade' => true])
        ->get('/dashboard')
        ->assertRedirect('/setup/password');
});