<?php

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

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

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

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

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

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

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