<?php

namespace Tests\Feature;

use App\Mail\Welcome;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Mail;
use Tests\TestCase;

class RegistrationTest extends TestCase
{
    use RefreshDatabase;

    private function assertRedirectsToPackages($response, ?int $packageId = null): void
    {
        $params = array_filter([
            'locale' => 'de',
            'package_id' => $packageId,
        ]);

        $response->assertRedirect(route('packages', $params));
    }

    public function test_registration_creates_user_and_tenant(): void
    {
        $response = $this->post(route('register.store'), [
            'username' => 'testuser',
            'email' => 'test@example.com',
            'password' => 'Password123!',
            'password_confirmation' => 'Password123!',
            'first_name' => 'Test',
            'last_name' => 'User',
            'address' => 'Test Address',
            'phone' => '123456789',
            'privacy_consent' => true,
        ]);

        $this->assertGuest();
        $this->assertRedirectsToPackages($response);
        $this->assertDatabaseMissing('users', [
            'email' => 'test@example.com',
        ]);
    }

    public function test_registration_without_package_assigns_tenant_only(): void
    {
        $response = $this->post(route('register.store'), [
            'username' => 'testuser2',
            'email' => 'test2@example.com',
            'password' => 'Password123!',
            'password_confirmation' => 'Password123!',
            'first_name' => 'Test',
            'last_name' => 'User',
            'address' => 'Test Address',
            'phone' => '123456789',
            'privacy_consent' => true,
        ]);

        $this->assertGuest();
        $this->assertRedirectsToPackages($response);
        $this->assertDatabaseMissing('users', [
            'email' => 'test2@example.com',
        ]);
    }

    public function test_registration_validation_fails(): void
    {
        $response = $this->post(route('register.store'), [
            'username' => '',
            'email' => 'invalid',
            'password' => 'short',
            'password_confirmation' => 'different',
            'first_name' => '',
            'last_name' => '',
            'address' => '',
            'phone' => '',
            'privacy_consent' => false,
        ]);

        $this->assertRedirectsToPackages($response);
        $this->assertDatabaseMissing('users', [
            'email' => 'invalid',
        ]);
    }

    public function test_registration_with_paid_package_redirects_to_checkout_flow(): void
    {
        $paidPackageId = 789;

        $response = $this->post(route('register.store'), [
            'username' => 'paiduser',
            'email' => 'paid@example.com',
            'password' => 'Password123!',
            'password_confirmation' => 'Password123!',
            'first_name' => 'Paid',
            'last_name' => 'User',
            'address' => 'Paid Address',
            'phone' => '123456789',
            'privacy_consent' => true,
            'package_id' => $paidPackageId,
        ]);

        $this->assertGuest();
        $this->assertRedirectsToPackages($response, $paidPackageId);
        $this->assertDatabaseMissing('users', [
            'email' => 'paid@example.com',
        ]);
    }

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

        $this->post(route('register.store'), [
            'username' => 'testuser3',
            'email' => 'test3@example.com',
            'password' => 'Password123!',
            'password_confirmation' => 'Password123!',
            'first_name' => 'Test',
            'last_name' => 'User',
            'address' => 'Test Address',
            'phone' => '123456789',
            'privacy_consent' => true,
        ]);

        Mail::assertNotSent(Welcome::class);
    }
}
