<?php

use App\Enums\IsActive;
use App\Livewire\Studio\Loyalty\Voucher\Index;
use App\Models\Voucher;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;

uses(RefreshDatabase::class);

it('renders successfully', function () {
    Livewire::test(Index::class)
        ->assertViewIs('livewire.studio.loyalty.voucher.index')
        ->assertViewHas('pageTitle', 'Voucher');
});

it('displays all vouchers', function () {
    Voucher::factory()
        ->count(10)
        ->state(fn () => [
            'is_active' => fake()->randomElement(IsActive::cases()),
        ])
        ->create();

    $voucher = Voucher::first();

    Livewire::test(Index::class)->assertSee($voucher->name);
});

it('can delete a voucher', function () {
    $voucher = Voucher::factory()
        ->state(fn () => [
            'is_active' => fake()->randomElement(IsActive::cases()),
        ])
        ->create();

    Livewire::test(Index::class)
        ->call('delete', $voucher)
        ->assertHasNoErrors()
        ->assertDispatched('refreshDatatable');

    expect(Voucher::count())->toBe(0);
});
