<?php

use App\Enums\Gender;
use App\Livewire\Member\Setting\Account;
use App\Models\Customer;
use Illuminate\Support\Facades\Hash;
use Livewire\Livewire;

beforeEach(function () {
    $this->setupUser();
});

it('renders member setting account page', function () {
    $this->actingAs($this->user);

    Livewire::test(Account::class)
        ->assertStatus(200)
        ->assertSee('Pengaturan Akun');
});

it('can update account information', function () {
    $this->actingAs($this->user);

    Livewire::test(Account::class)
        ->set('accountForm.username', 'newusername')
        ->set('accountForm.email', 'new@example.com')
        ->call('updateAccount')
        ->assertHasNoErrors();

    $this->assertDatabaseHas('users', [
        'id' => $this->user->id,
        'username' => 'newusername',
        'email' => 'new@example.com',
    ]);
});

it('can update member profile when customer exists', function () {
    $this->actingAs($this->user);
    $customer = Customer::factory()->for($this->user)->create([
        'name' => 'Old Name',
        'phone_number' => '0812 3456 7890',
        'gender' => Gender::MALE,
    ]);

    Livewire::test(Account::class)
        ->set('profileForm.name', 'New Name')
        ->set('profileForm.phone_number', '0812 3456 7891')
        ->set('profileForm.gender', Gender::FEMALE->value)
        ->call('updateProfile')
        ->assertHasNoErrors();

    $this->assertDatabaseHas('customers', [
        'id' => $customer->id,
        'name' => 'New Name',
        'phone_number' => '0812 3456 7891',
        'gender' => Gender::FEMALE->value,
    ]);
});

it('can change password', function () {
    $this->actingAs($this->user);
    $this->user->update(['password' => Hash::make('OldPassword123!')]);

    Livewire::test(Account::class)
        ->set('passwordForm.current_password', 'OldPassword123!')
        ->set('passwordForm.new_password', '@Member#2026_Settings!')
        ->set('passwordForm.new_confirm_password', '@Member#2026_Settings!')
        ->call('updatePassword')
        ->assertHasNoErrors()
        ->assertRedirect(route('login'));
});
