<?php

use App\Livewire\Studio\Master\Warehouse\Index;
use App\Models\Warehouse;
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 warehouse']);
    Permission::firstOrCreate(['name' => 'create warehouse']);
    Permission::firstOrCreate(['name' => 'update warehouse']);
    Permission::firstOrCreate(['name' => 'delete warehouse']);
    $role->givePermissionTo(['view warehouse', 'create warehouse', 'update warehouse', 'delete warehouse']);
    $this->user->assignRole($role);
});

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

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

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

it('can store a new warehouse', function () {
    Livewire::actingAs($this->user)
        ->test(Index::class)
        ->set('form.name', 'Gudang Baru')
        ->set('form.description', 'Deskripsi Gudang Baru')
        ->call('create')
        ->assertHasNoErrors()
        ->assertDispatched('refreshDatatable');

    $this->assertDatabaseHas('warehouses', [
        'name' => 'Gudang Baru',
        'description' => 'Deskripsi Gudang Baru',
    ]);
});

it('can open edit modal and load data', function () {
    $warehouse = Warehouse::factory()->create(['name' => 'Gudang Edit']);

    Livewire::actingAs($this->user)
        ->test(Index::class)
        ->call('openModal', 'update', 'Ubah Gudang', $warehouse->hash_id)
        ->set('form.warehouse', $warehouse)
        ->set('form.name', $warehouse->name)
        ->assertSet('method', 'update')
        ->assertSet('modalTitle', 'Ubah Gudang')
        ->assertSet('form.name', 'Gudang Edit');
});

it('can update a warehouse', function () {
    $warehouse = Warehouse::factory()->create(['name' => 'Gudang Lama']);

    Livewire::actingAs($this->user)
        ->test(Index::class)
        ->call('openModal', 'update', 'Ubah Gudang', $warehouse->hash_id)
        ->set('form.warehouse', $warehouse)
        ->set('form.name', 'Gudang Diperbarui')
        ->call('update')
        ->assertHasNoErrors()
        ->assertDispatched('refreshDatatable');

    $this->assertDatabaseHas('warehouses', [
        'id' => $warehouse->id,
        'name' => 'Gudang Diperbarui',
    ]);
});

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

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

    $this->assertSoftDeleted('warehouses', ['id' => $warehouse->id]);
});

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

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