<?php

namespace Tests\Feature\Auth;

use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
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_screen_can_be_rendered(): void
    {
        $response = $this->get(route('register'));

        $this->assertRedirectsToPackages($response);
    }

    public function test_new_users_can_register(): void
    {
        $response = $this->post(route('register.store'), [
            'name' => 'Test User',
            'username' => 'testuser',
            'email' => 'test@example.com',
            'password' => 'Password123!',
            'password_confirmation' => 'Password123!',
            'first_name' => 'Max',
            'last_name' => 'Mustermann',
            'address' => 'Musterstr. 1',
            'phone' => '+49123456789',
            'privacy_consent' => true,
        ]);

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

    public function test_registration_with_free_package_assigns_tenant_package(): void
    {
        $freePackageId = 123;

        $response = $this->post(route('register.store'), [
            'name' => 'Test User',
            'username' => 'testuserfree',
            'email' => 'free@example.com',
            'password' => 'Password123!',
            'password_confirmation' => 'Password123!',
            'first_name' => 'Max',
            'last_name' => 'Mustermann',
            'address' => 'Musterstr. 1',
            'phone' => '+49123456789',
            'privacy_consent' => true,
            'package_id' => $freePackageId,
        ]);

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

    public function test_registration_with_paid_package_redirects_to_buy(): void
    {
        $paidPackageId = 456;

        $response = $this->post(route('register.store'), [
            'name' => 'Test User',
            'username' => 'testuserpaid',
            'email' => 'paid@example.com',
            'password' => 'Password123!',
            'password_confirmation' => 'Password123!',
            'first_name' => 'Max',
            'last_name' => 'Mustermann',
            'address' => 'Musterstr. 1',
            'phone' => '+49123456789',
            'privacy_consent' => true,
            'package_id' => $paidPackageId,
        ]);

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

    public function test_registration_fails_with_invalid_email(): void
    {
        $response = $this->post(route('register.store'), [
            'name' => 'Test User',
            'username' => 'invaliduser',
            'email' => 'invalid-email',
            'password' => 'password',
            'password_confirmation' => 'password',
            'first_name' => 'Max',
            'last_name' => 'Mustermann',
            'address' => 'Musterstr. 1',
            'phone' => '+49123456789',
            'privacy_consent' => true,
        ]);

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

    public function test_registration_success_sets_status_flash(): void
    {
        $response = $this->post(route('register.store'), [
            'name' => 'Test User',
            'username' => 'successreg',
            'email' => 'successreg@example.com',
            'password' => 'Password123!',
            'password_confirmation' => 'Password123!',
            'first_name' => 'Max',
            'last_name' => 'Mustermann',
            'address' => 'Musterstr. 1',
            'phone' => '+49123456789',
            'privacy_consent' => true,
        ]);

        $this->assertGuest();
        $this->assertRedirectsToPackages($response);
    }

    public function test_registration_fails_with_short_password(): void
    {
        $response = $this->post(route('register.store'), [
            'name' => 'Test User',
            'username' => 'shortpass',
            'email' => 'short@example.com',
            'password' => 'short',
            'password_confirmation' => 'short',
            'first_name' => 'Max',
            'last_name' => 'Mustermann',
            'address' => 'Musterstr. 1',
            'phone' => '+49123456789',
            'privacy_consent' => true,
        ]);

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

    public function test_registration_fails_without_privacy_consent(): void
    {
        $response = $this->post(route('register.store'), [
            'name' => 'Test User',
            'username' => 'noconsent',
            'email' => 'noconsent@example.com',
            'password' => 'Password123!',
            'password_confirmation' => 'Password123!',
            'first_name' => 'Max',
            'last_name' => 'Mustermann',
            'address' => 'Musterstr. 1',
            'phone' => '+49123456789',
            'privacy_consent' => false,
        ]);

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

    public function test_registration_fails_with_duplicate_email(): void
    {
        User::factory()->create(['email' => 'duplicate@example.com']);

        $response = $this->post(route('register.store'), [
            'name' => 'Duplicate User',
            'username' => 'duplicate',
            'email' => 'duplicate@example.com',
            'password' => 'Password123!',
            'password_confirmation' => 'Password123!',
            'first_name' => 'Max',
            'last_name' => 'Mustermann',
            'address' => 'Musterstr. 1',
            'phone' => '+49123456789',
            'privacy_consent' => true,
        ]);

        $this->assertRedirectsToPackages($response);
    }

    public function test_registration_fails_with_mismatched_passwords(): void
    {
        $response = $this->post(route('register.store'), [
            'name' => 'Test User',
            'username' => 'mismatch',
            'email' => 'mismatch@example.com',
            'password' => 'Password123!',
            'password_confirmation' => 'different123!',
            'first_name' => 'Max',
            'last_name' => 'Mustermann',
            'address' => 'Musterstr. 1',
            'phone' => '+49123456789',
            'privacy_consent' => true,
        ]);

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

    public function test_registration_with_invalid_package_id_triggers_validation_error(): void
    {
        $response = $this->post(route('register.store'), [
            'name' => 'Test User',
            'username' => 'invalidpkg',
            'email' => 'invalidpkg@example.com',
            'password' => 'Password123!',
            'password_confirmation' => 'Password123!',
            'first_name' => 'Max',
            'last_name' => 'Mustermann',
            'address' => 'Musterstr. 1',
            'phone' => '+49123456789',
            'privacy_consent' => true,
            'package_id' => 999,
        ]);

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