<?php

use App\Livewire\Studio\Feature\PerfumeFeature;
use App\Models\PerfumeFeature as PerfumeFeatureModel;
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 perfume feature']));
    $role->givePermissionTo(Permission::create(['name' => 'update perfume feature']));
    $role->givePermissionTo(Permission::create(['name' => 'delete perfume feature']));
    $this->user->assignRole($role);
});

it('renders the perfume feature page', function () {
    Livewire::actingAs($this->user)
        ->test(PerfumeFeature::class)
        ->assertStatus(200)
        ->assertViewIs('livewire.studio.feature.perfume-features')
        ->assertSee('Keunggulan Parfum');
});

it('can create a perfume feature', function () {
    Livewire::actingAs($this->user)
        ->test(PerfumeFeature::class)
        ->call('openModal', 'create', 'Tambah Keunggulan')
        ->set('form.name', 'Tahan Lama')
        ->set('form.description', 'Wangi tahan hingga 24 jam')
        ->call('create')
        ->assertDispatched('refreshDatatable')
        // ->assertDispatched('toast', message: 'Keunggulan parfum berhasil ditambahkan.')
        ->assertHasNoErrors();

    $this->assertDatabaseHas('perfume_features', [
        'name' => 'Tahan Lama',
        'description' => 'Wangi tahan hingga 24 jam',
    ]);
});

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

it('can update a perfume feature', function () {
    $feature = PerfumeFeatureModel::factory()->create([
        'name' => 'Original',
        'description' => 'Old Description',
    ]);

    Livewire::actingAs($this->user)
        ->test(PerfumeFeature::class)
        ->call('openModal', 'update', 'Edit Keunggulan', $feature->hash)
        ->assertSet('form.name', 'Original')
        ->assertSet('form.description', 'Old Description')
        ->set('form.name', 'Updated Name')
        ->set('form.description', 'New Description')
        ->call('update')
        ->assertDispatched('refreshDatatable')
        // ->assertDispatched('toast', message: 'Keunggulan parfum berhasil diperbarui.')
        ->assertHasNoErrors();

    $this->assertDatabaseHas('perfume_features', [
        'id' => $feature->id,
        'name' => 'Updated Name',
        'description' => 'New Description',
    ]);
});

it('can delete a perfume feature', function () {
    $feature = PerfumeFeatureModel::factory()->create();

    Livewire::actingAs($this->user)
        ->test(PerfumeFeature::class)
        ->call('delete', $feature)
        ->assertDispatched('refreshDatatable')
        // ->assertDispatched('toast', message: 'Keunggulan parfum berhasil dihapus.')
        ->assertHasNoErrors();

    $this->assertSoftDeleted('perfume_features', [
        'id' => $feature->id,
    ]);
});

it('can sort perfume features', function () {
    $feature1 = PerfumeFeatureModel::factory()->create(['sort_order' => 1]);
    $feature2 = PerfumeFeatureModel::factory()->create(['sort_order' => 2]);

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

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

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

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

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