<?php

namespace Tests\Feature\Auth;

use App\Enums\UserKycStatus;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Fortify\Features;
use Tests\TestCase;

class RegistrationTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();

        $this->skipUnlessFortifyHas(Features::registration());
    }

    public function test_registration_screen_can_be_rendered()
    {
        $response = $this->get(route('register'));

        $response->assertOk();
    }

    public function test_first_registered_user_is_admin_when_database_is_empty()
    {
        $this->post(route('register.store'), [
            'first_name' => 'Ada',
            'last_name' => 'Lovelace',
            'email' => 'ada@example.com',
            'contact_number' => '+1 555 100 1000',
            'country' => 'US',
            'password' => 'password',
            'password_confirmation' => 'password',
        ])->assertRedirect(route('dashboard', absolute: false));

        $this->assertAuthenticated();
        $ada = User::query()->where('email', 'ada@example.com')->first();
        $this->assertNotNull($ada);
        $this->assertTrue($ada->is_admin);
        $this->assertSame(UserKycStatus::Approved, $ada->kyc_status);
    }

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

        $this->post(route('register.store'), [
            'first_name' => 'Bob',
            'last_name' => 'Builder',
            'email' => 'bob@example.com',
            'contact_number' => '+1 555 200 2000',
            'country' => 'US',
            'password' => 'password',
            'password_confirmation' => 'password',
        ])->assertRedirect(route('dashboard', absolute: false));

        $this->assertAuthenticated();
        $bob = User::query()->where('email', 'bob@example.com')->first();
        $this->assertNotNull($bob);
        $this->assertFalse($bob->is_admin);
        $this->assertSame(UserKycStatus::RequiresSubmission, $bob->kyc_status);
    }

    public function test_new_users_can_register()
    {
        $response = $this->post(route('register.store'), [
            'first_name' => 'Test',
            'last_name' => 'User',
            'email' => 'test@example.com',
            'contact_number' => '+1 555 123 4567',
            'country' => 'US',
            'password' => 'password',
            'password_confirmation' => 'password',
        ]);

        $this->assertAuthenticated();
        $response->assertRedirect(route('dashboard', absolute: false));
    }

    public function test_registration_rejects_referral_code_when_referrer_program_disabled(): void
    {
        User::factory()->create(['email' => 'seed@example.com']);
        $referrer = User::factory()->create([
            'email' => 'ref-disabled@example.com',
            'referral_program_enabled' => false,
        ]);

        $response = $this->from(route('register'))->post(route('register.store'), [
            'first_name' => 'Test',
            'last_name' => 'User',
            'email' => 'new-inv@example.com',
            'contact_number' => '+1 555 987 0000',
            'country' => 'US',
            'password' => 'password',
            'password_confirmation' => 'password',
            'referral_code' => $referrer->referral_code,
        ]);

        $response->assertSessionHasErrors('referral_code');
    }
}
