<?php

declare(strict_types=1);

namespace Tests\Feature\Demo;

use App\Mail\DemoCredentialsMail;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Mail;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;

class DemoRequestTest extends TestCase
{
    use DatabaseTransactions;

    #[Test]
    public function the_demo_page_renders(): void
    {
        $this->get('/demo')->assertOk()->assertSee('credenciales', false);
    }

    #[Test]
    public function submitting_captures_a_lead_and_queues_the_credentials_email_without_logging_in(): void
    {
        Mail::fake();

        $response = $this->post('/demo', [
            'name' => 'Prospecto Uno',
            'email' => 'prospecto@example.com',
            'phone' => '3001234567',
        ]);

        $response->assertRedirect();
        $response->assertSessionHas('demo_sent', true);
        $this->assertGuest(); // sin auto-login
        $this->assertDatabaseHas('leads', ['email' => 'prospecto@example.com', 'source' => 'demo_request']);
        Mail::assertQueued(DemoCredentialsMail::class, fn ($m) => $m->hasTo('prospecto@example.com'));
    }
}
