<?php

use App\Livewire\Member\Membership;
use App\Models\Membership as MembershipModel;
use App\Models\Tier;
use Livewire\Livewire;

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

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

    $tier = Tier::factory()->create(['min_spending' => 0, 'name' => 'Bronze']);
    MembershipModel::factory()->for($this->user)->create([
        'tier_id' => $tier->id,
        'reward_points' => 100,
        'total_spending' => 50000,
    ]);

    Livewire::test(Membership::class)
        ->assertStatus(200)
        ->assertSee('Membership')
        ->assertSee('Bronze')
        ->assertSee('100');
});

it('shows max tier if already at highest tier', function () {
    $this->actingAs($this->user);

    $tier = Tier::factory()->create(['min_spending' => 0, 'name' => 'Bronze']);
    MembershipModel::factory()->for($this->user)->create([
        'tier_id' => $tier->id,
        'total_spending' => 1000000,
    ]);

    Livewire::test(Membership::class)
        ->assertSet('nextTierName', 'Maksimal')
        ->assertSet('progress', '100');
});

it('calculates progress to next tier correctly', function () {
    $this->actingAs($this->user);

    $bronze = Tier::factory()->create(['min_spending' => 0, 'name' => 'Bronze']);
    $silver = Tier::factory()->create(['min_spending' => 100000, 'name' => 'Silver']);

    MembershipModel::factory()->for($this->user)->create([
        'tier_id' => $bronze->id,
        'total_spending' => 50000,
    ]);

    Livewire::test(Membership::class)
        ->assertSet('nextTierName', 'Silver')
        ->assertSet('progress', 50);
});
