<?php

use App\Livewire\Studio\Manage\Article\Index;
use App\Models\Article;
use Livewire\Livewire;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;

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

    // Create necessary permissions
    $permissions = [
        'view article',
        'delete article',
    ];

    foreach ($permissions as $permission) {
        Permission::firstOrCreate(['name' => $permission]);
    }

    $role = Role::create(['name' => 'Writer']);
    $role->givePermissionTo($permissions);
    $this->user->assignRole($role);
});

it('renders the article index page', function () {
    Livewire::actingAs($this->user)
        ->test(Index::class)
        ->assertStatus(200);
});

it('can delete an article', function () {
    $article = Article::factory()->create(['author_id' => $this->user->id]);

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

    $this->assertSoftDeleted('articles', ['id' => $article->id]);
});
