<?php

use App\Enums\Gender;
use App\Livewire\Auth\Register;
use App\Models\ReferralCode;
use App\Models\Tier;
use App\Models\User;
use Illuminate\Support\Facades\Notification;
use Livewire\Livewire;
use Spatie\Permission\Models\Role;

beforeEach(function () {
    Notification::fake();

    // Required for registration logic
    Tier::factory()->create(['min_spending' => 0]);

    // Required for notification logic in Register component
    Role::firstOrCreate(['name' => 'Developer']);
    Role::firstOrCreate(['name' => 'Owner']);
    Role::firstOrCreate(['name' => 'Customer']);
});

it('renders the register page correctly', function () {
    $this->get(route('register'))
        ->assertOk()
        ->assertSeeLivewire(Register::class);
});

it('shows validation error when step 1 fields are empty', function () {
    Livewire::test(Register::class)
        ->call('nextStep')
        ->assertHasErrors([
            'form.email' => 'required',
            'form.username' => 'required',
            'form.password' => 'required',
        ]);
});

it('shows validation error for invalid email and short username', function () {
    Livewire::test(Register::class)
        ->set('form.email', 'not-an-email')
        ->set('form.username', 'abc')
        ->call('nextStep')
        ->assertHasErrors([
            'form.email' => 'email',
            'form.username' => 'min',
        ]);
});

it('can move to step 2 with valid step 1 data', function () {
    Livewire::test(Register::class)
        ->set('form.email', 'newuser@example.com')
        ->set('form.username', 'newuser')
        ->set('form.password', 'S7r1ct_P@ssw0rd_2026!')
        ->call('nextStep')
        ->assertHasNoErrors()
        ->assertSet('currentStep', 2);
});

it('shows validation error for step 2 fields', function () {
    Livewire::test(Register::class)
        ->set('currentStep', 2)
        ->call('register')
        ->assertHasErrors([
            'form.name' => 'required',
            'form.phone_number' => 'required',
            'form.gender' => 'required',
        ]);
});

it('can register a new user successfully', function () {
    Livewire::test(Register::class)
        // Step 1
        ->set('form.email', 'success@example.com')
        ->set('form.username', 'successuser')
        ->set('form.password', 'S7r1ct_P@ssw0rd_2026!')
        ->call('nextStep')
        // Step 2
        ->set('form.name', 'Success User')
        ->set('form.phone_number', '0812 3456 7890')
        ->set('form.gender', Gender::MALE->value)
        ->call('register')
        ->assertHasNoErrors()
        ->assertRedirect('/member/overview');

    $this->assertDatabaseHas('users', [
        'email' => 'success@example.com',
        'username' => 'successuser',
    ]);

    $user = User::where('email', 'success@example.com')->first();
    $this->assertAuthenticatedAs($user);
    $this->assertTrue($user->hasRole('Customer'));
});

it('can register with a valid referral code', function () {
    // Setup a user with a referral code
    $this->setupUser(['username' => 'referrer']);
    $referral = ReferralCode::create([
        'user_id' => $this->user->id,
        'code' => 'REF123',
    ]);

    Livewire::test(Register::class)
        // Step 1
        ->set('form.email', 'referred@example.com')
        ->set('form.username', 'referreduser')
        ->set('form.password', 'S7r1ct_P@ssw0rd_2026!')
        ->call('nextStep')
        // Step 2
        ->set('form.name', 'Referred User')
        ->set('form.phone_number', '0899 8888 7777')
        ->set('form.gender', Gender::FEMALE->value)
        ->set('form.referral_code', 'REF123')
        ->call('register')
        ->assertHasNoErrors()
        ->assertRedirect('/member/overview');

    $this->assertDatabaseHas('referral_usages', [
        'referral_code_id' => $referral->id,
    ]);
});
