<?php

namespace Tests\Feature;

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

class CheckoutRegisterRoleTest extends TestCase
{
    use RefreshDatabase;

    public function test_checkout_register_sets_user_role_until_purchase(): void
    {
        Mail::fake();

        $package = Package::factory()->create();

        $payload = [
            'email' => 'buyer@example.test',
            'password' => 'Password!123',
            'password_confirmation' => 'Password!123',
            'first_name' => 'Soren',
            'last_name' => 'Eberhardt',
            'package_id' => $package->id,
            'terms' => true,
            'privacy_consent' => true,
            'locale' => 'de',
        ];

        $response = $this->postJson('/checkout/register', $payload);

        $response->assertOk();

        $user = User::where('email', 'buyer@example.test')->first();

        $this->assertNotNull($user);
        $this->assertSame('user', $user->role);
        $this->assertSame('buyer@example.test', $user->username);
    }
}
