<?php

use App\Mail\RegistrationVerificationMail;
use App\Models\User;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Queue;

uses(RefreshDatabase::class);

it('registration verification mailable is queued with retry policy', function () {
    $mail = new RegistrationVerificationMail('token-123');

    expect($mail)->toBeInstanceOf(ShouldQueue::class);
    expect($mail->tries)->toBe(3);
    expect($mail->timeout)->toBe(30);
    expect($mail->backoff)->toBe([60, 300, 900]);
});

it('registration email contains verification link expiry and support url', function () {
    config()->set('app.url', 'https://skinbase.example');
    config()->set('app.name', 'Skinbase');

    $mail = new RegistrationVerificationMail('abc-token');
    $html = $mail->render();

    expect($html)->toContain('Verify Email');
    expect($html)->toContain('/verify/abc-token');
    expect($html)->toContain('expires in 24 hours');
    expect($html)->toContain('https://skinbase.example/support');
});

it('registration endpoint skips verification email job and continues onboarding immediately', function () {
    Queue::fake();

    $response = $this->post('/register', [
        'email' => 'mail-test@example.com',
    ]);

    $user = User::query()->where('email', 'mail-test@example.com')->firstOrFail();

    $this->assertAuthenticatedAs($user);
    $response->assertRedirect('/setup/password');
    Queue::assertNothingPushed();
    $this->assertDatabaseHas('users', [
        'email' => 'mail-test@example.com',
        'onboarding_step' => 'verified',
    ]);
});
