<?php

use App\Enums\CategoryType;
use App\Livewire\Studio\Master\Category;
use App\Models\Category as CategoryModel;
use Livewire\Livewire;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;

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

    $role = Role::firstOrCreate(['name' => 'Owner']);
    Permission::firstOrCreate(['name' => 'view category']);
    Permission::firstOrCreate(['name' => 'create category']);
    Permission::firstOrCreate(['name' => 'update category']);
    Permission::firstOrCreate(['name' => 'delete category']);
    $role->givePermissionTo(['view category', 'create category', 'update category', 'delete category']);
    $this->user->assignRole($role);
});

it('renders the category page correctly', function () {
    $this->actingAs($this->user)
        ->get(route('studio.master.category.index'))
        ->assertOk()
        ->assertSeeLivewire(Category::class);
});

it('can open create modal', function () {
    Livewire::actingAs($this->user)
        ->test(Category::class)
        ->dispatch('modal:open', method: 'create', modalTitle: 'Tambah Kategori')
        ->assertSet('method', 'create')
        ->assertSet('modalTitle', 'Tambah Kategori');
});

it('validates required fields on create', function () {
    Livewire::actingAs($this->user)
        ->test(Category::class)
        ->call('create')
        ->assertHasErrors(['form.name' => 'required']);
});

it('can store a new category', function () {
    Livewire::actingAs($this->user)
        ->test(Category::class)
        ->set('form.name', 'Kategori Baru')
        ->set('form.type', CategoryType::PERFUME->value)
        ->set('form.description', 'Deskripsi Kategori')
        ->call('create')
        ->assertHasNoErrors()
        ->assertDispatched('refreshDatatable');

    $this->assertDatabaseHas('categories', [
        'name' => 'Kategori Baru',
        'type' => CategoryType::PERFUME->value,
        'description' => 'Deskripsi Kategori',
    ]);
});

it('can open edit modal and load data', function () {
    $category = CategoryModel::factory()->create([
        'name' => 'Kategori Edit',
        'type' => CategoryType::ARTICLE->value,
    ]);

    Livewire::actingAs($this->user)
        ->test(Category::class)
        ->call('openModal', 'update', 'Ubah Kategori', $category->hash_id)
        ->set('form.category', $category)
        ->set('form.name', $category->name)
        ->set('form.type', $category->type->value)
        ->assertSet('method', 'update')
        ->assertSet('modalTitle', 'Ubah Kategori')
        ->assertSet('form.name', 'Kategori Edit')
        ->assertSet('form.type', CategoryType::ARTICLE->value);
});

it('can update a category', function () {
    $category = CategoryModel::factory()->create(['name' => 'Kategori Lama', 'type' => CategoryType::PERFUME->value]);

    Livewire::actingAs($this->user)
        ->test(Category::class)
        ->call('openModal', 'update', 'Ubah Kategori', $category->hash_id)
        ->set('form.category', $category)
        ->set('form.name', 'Kategori Diperbarui')
        ->set('form.type', $category->type->value)
        ->call('update')
        ->assertHasNoErrors()
        ->assertDispatched('refreshDatatable');

    $this->assertDatabaseHas('categories', [
        'id' => $category->id,
        'name' => 'Kategori Diperbarui',
    ]);
});

it('can delete a category', function () {
    $category = CategoryModel::factory()->create();

    Livewire::actingAs($this->user)
        ->test(Category::class)
        ->call('delete', $category->id)
        ->assertDispatched('refreshDatatable');

    $this->assertSoftDeleted('categories', ['id' => $category->id]);
});

it('can change sort order', function () {
    $category = CategoryModel::factory()->create(['sort_order' => 2]);

    Livewire::actingAs($this->user)
        ->test(Category::class)
        ->call('sortOrder', $category->id, 'up')
        ->assertDispatched('refreshDatatable');

    $category->refresh();
    // Assuming 'up' decreases sort_order if 1 is top
    // Let's verify how sortOrder is implemented in CategoryForm/WithSortOrder
});

it('cannot access category page without permission', function () {
    $this->user->roles()->detach();
    $this->user->permissions()->detach();

    $this->actingAs($this->user)
        ->get(route('studio.master.category.index'))
        ->assertForbidden();
});
