<?php

use App\Livewire\Studio\Feature\ChooseUs;
use App\Models\ChooseUs as ChooseUsModel;
use App\Models\User;
use Livewire\Livewire;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;

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

    // Setup Permissions
    $role = Role::create(['name' => 'super-admin']);
    $role->givePermissionTo(Permission::create(['name' => 'create choose us']));
    $role->givePermissionTo(Permission::create(['name' => 'update choose us']));
    $role->givePermissionTo(Permission::create(['name' => 'delete choose us']));
    $this->user->assignRole($role);
});

it('renders the choose us page', function () {
    Livewire::actingAs($this->user)
        ->test(ChooseUs::class)
        ->assertStatus(200)
        ->assertViewIs('livewire.studio.feature.choose-us')
        ->assertSee('Pilih Kami');
});

it('can create a choose us item', function () {
    Livewire::actingAs($this->user)
        ->test(ChooseUs::class)
        ->call('openModal', 'create', 'Tambah Pilih Kami')
        ->set('form.name', 'Kualitas Terbaik')
        ->set('form.description', 'Kami menjamin kualitas.')
        ->set('form.icon', 'star')
        ->call('create')
        ->assertDispatched('refreshDatatable')
        // ->assertDispatched('toast', message: 'Pilih kami berhasil ditambahkan.')
        ->assertHasNoErrors();

    $this->assertDatabaseHas('choose_us', [
        'name' => 'Kualitas Terbaik',
        'description' => 'Kami menjamin kualitas.',
        'icon' => 'star',
    ]);
});

it('validates required fields when creating', function () {
    Livewire::actingAs($this->user)
        ->test(ChooseUs::class)
        ->call('openModal', 'create', 'Tambah Pilih Kami')
        ->set('form.name', '')
        ->set('form.description', '')
        ->set('form.icon', '')
        ->call('create')
        ->assertHasErrors([
            'form.name' => 'required',
            'form.description' => 'required',
            'form.icon' => 'required',
        ]);
});

it('can update a choose us item', function () {
    $item = ChooseUsModel::factory()->create([
        'name' => 'Original',
        'description' => 'Old Description',
        'icon' => 'star',
    ]);

    Livewire::actingAs($this->user)
        ->test(ChooseUs::class)
        ->call('openModal', 'update', 'Edit Pilih Kami', $item->hash)
        ->assertSet('form.name', 'Original')
        ->assertSet('form.description', 'Old Description')
        ->assertSet('form.icon', 'star')
        ->set('form.name', 'Updated Name')
        ->set('form.description', 'New Description')
        ->set('form.icon', 'heart')
        ->call('update')
        ->assertDispatched('refreshDatatable')
        // ->assertDispatched('toast', message: 'Pilih kami berhasil diperbarui.')
        ->assertHasNoErrors();

    $this->assertDatabaseHas('choose_us', [
        'id' => $item->id,
        'name' => 'Updated Name',
        'description' => 'New Description',
        'icon' => 'heart',
    ]);
});

it('can delete a choose us item', function () {
    $item = ChooseUsModel::factory()->create();

    Livewire::actingAs($this->user)
        ->test(ChooseUs::class)
        ->call('delete', $item)
        ->assertDispatched('refreshDatatable')
        // ->assertDispatched('toast', message: 'Pilih kami berhasil dihapus.')
        ->assertHasNoErrors();

    $this->assertSoftDeleted('choose_us', [
        'id' => $item->id,
    ]);
});

it('can sort choose us items', function () {
    $item1 = ChooseUsModel::factory()->create(['sort_order' => 1]);
    $item2 = ChooseUsModel::factory()->create(['sort_order' => 2]);

    Livewire::actingAs($this->user)
        ->test(ChooseUs::class)
        ->call('sortOrder', $item1, 'down')
        ->assertDispatched('refreshDatatable');

    $this->assertDatabaseHas('choose_us', [
        'id' => $item1->id,
        'sort_order' => 2,
    ]);

    $this->assertDatabaseHas('choose_us', [
        'id' => $item2->id,
        'sort_order' => 1,
    ]);
});

it('cannot access without permission', function () {
    $userWithoutPermission = User::factory()->create();

    Livewire::actingAs($userWithoutPermission)
        ->test(ChooseUs::class)
        ->call('create')
        ->assertForbidden();
});
