<?php

use App\Livewire\Studio\Loyalty\Voucher\Create;
use App\Livewire\Studio\Loyalty\Voucher\Index;
use App\Models\Facility;
use App\Models\OpeningHour;
use App\Models\Outlet;
use App\Models\Voucher;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;

uses(RefreshDatabase::class);

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

it('can create a voucher', function () {
    $data = Voucher::factory()
        ->active()
        ->raw();

    $outletIds = Outlet::factory()
        ->count(4)
        ->has(OpeningHour::factory()->count(7))
        ->has(Facility::factory()->count(5))
        ->create();

    Livewire::test(Create::class)
        ->set('form.name', $data['name'])
        ->set('form.code', $data['code'])
        ->set('form.min_purchase', $data['min_purchase'])
        ->set('form.quota', $data['quota'])
        ->set('form.limit_per_user', $data['limit_per_user'])
        ->set('form.start_date', $data['start_date'])
        ->set('form.end_date', $data['end_date'])
        ->set('form.type', $data['type'])
        ->set('form.discount_amount', $data['discount_amount'])
        ->set('form.max_discount', $data['max_discount'])
        ->set('form.is_active', $data['is_active']->value)
        ->set('form.outlet_ids', $outletIds->pluck('id')->toArray())
        ->call('save')
        ->assertHasNoErrors()
        ->assertDispatched('refreshDatatable')
        ->assertRedirect(Index::class);

    $this->assertDatabaseHas('vouchers', [
        'name' => $data['name'],
        'code' => $data['code'],
    ]);
});
