<?php

use App\Livewire\Studio\Catalog\Perfume\Audit;
use App\Models\Outlet;
use App\Models\Perfume;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;

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

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

it('renders the perfume audit page correctly', function () {
    $outlet = Outlet::factory()->create();
    $perfume = Perfume::factory()->create();

    $this->actingAs($this->user)
        ->get(route('studio.catalog.perfume.audit', ['outlet' => $outlet, 'perfume' => $perfume]))
        ->assertOk()
        ->assertSeeLivewire(Audit::class);
});

it('cannot access perfume audit without permission', function () {
    $outlet = Outlet::factory()->create();
    $perfume = Perfume::factory()->create();
    $this->user->roles()->detach();
    $this->user->permissions()->detach();

    $this->actingAs($this->user)
        ->get(route('studio.catalog.perfume.audit', ['outlet' => $outlet, 'perfume' => $perfume]))
        ->assertForbidden();
});
