<?php

use App\Enums\Gender;
use App\Livewire\Member\Overview;
use App\Models\Customer;
use App\Models\Tier;
use Livewire\Livewire;

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

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

    Livewire::test(Overview::class)
        ->assertStatus(200)
        ->assertSee('Selamat datang');
});

it('shows registration form if user is not a member', function () {
    $this->actingAs($this->user);

    Livewire::test(Overview::class)
        ->assertSet('hasMemberData', false)
        ->assertSee('Anda belum terdaftar menjadi member');
});

it('can register as a member', function () {
    $this->actingAs($this->user);

    // We need at least one tier for membership creation
    Tier::factory()->create(['min_spending' => 0]);

    Livewire::test(Overview::class)
        ->set('form.user_id', $this->user->id)
        ->set('form.name', 'Test Member')
        ->set('form.phone_number', '0812 3456 7890')
        ->set('form.gender', Gender::MALE->value)
        ->call('save')
        ->assertHasNoErrors()
        ->assertSet('hasMemberData', true);

    $this->assertDatabaseHas('customers', [
        'user_id' => $this->user->id,
        'name' => 'Test Member',
        'phone_number' => '0812 3456 7890',
        'gender' => Gender::MALE->value,
    ]);

    $this->assertDatabaseHas('memberships', [
        'user_id' => $this->user->id,
    ]);
});

it('shows member data if user is already a member', function () {
    $this->actingAs($this->user);

    Customer::factory()->for($this->user)->create();

    Livewire::test(Overview::class)
        ->assertSet('hasMemberData', true)
        ->assertDontSee('Daftar Member');
});
