<?php

use App\Domains\Bio\Models\BioPage;
use App\Domains\Workspace\Models\Workspace;
use App\Domains\Workspace\Models\WorkspaceMember;
use App\Livewire\Modals\CreateBioPage;
use App\Livewire\Modals\DeleteBioPage;
use App\Livewire\Modals\EditBioPage;
use App\Models\User;
use Livewire\Livewire;

function makeUserWithWsForBio(): array
{
    $user = User::factory()->create();
    $ws = Workspace::factory()->create(['owner_id' => $user->id]);
    WorkspaceMember::create([
        'workspace_id' => $ws->id,
        'user_id' => $user->id,
        'role' => 'owner',
        'joined_at' => now(),
    ]);

    return [$user, $ws];
}

// CreateBioPage

it('renders create bio page modal', function () {
    [$user, $ws] = makeUserWithWsForBio();
    app()->instance('current_workspace', $ws);

    Livewire::actingAs($user)->test(CreateBioPage::class)
        ->assertOk()
        ->assertSee('Bio Page');
});

it('validates required title on create bio page', function () {
    [$user, $ws] = makeUserWithWsForBio();
    app()->instance('current_workspace', $ws);

    Livewire::actingAs($user)->test(CreateBioPage::class)
        ->call('save')
        ->assertHasErrors(['title' => 'required']);
});

it('creates bio page and dispatches event', function () {
    [$user, $ws] = makeUserWithWsForBio();
    app()->instance('current_workspace', $ws);

    Livewire::actingAs($user)->test(CreateBioPage::class)
        ->set('title', 'My Bio Page')
        ->call('save')
        ->assertDispatched('bio-created')
        ->assertDispatched('toast');

    $this->assertDatabaseHas('bio_pages', [
        'workspace_id' => $ws->id,
    ]);
});

// EditBioPage

it('renders edit bio page modal with existing data', function () {
    [$user, $ws] = makeUserWithWsForBio();
    app()->instance('current_workspace', $ws);
    $bio = BioPage::factory()->create(['workspace_id' => $ws->id]);

    Livewire::actingAs($user)->test(EditBioPage::class, ['bioUlid' => $bio->ulid])
        ->assertOk();
});

it('updates bio page and dispatches event', function () {
    [$user, $ws] = makeUserWithWsForBio();
    app()->instance('current_workspace', $ws);
    $bio = BioPage::factory()->create(['workspace_id' => $ws->id]);

    Livewire::actingAs($user)->test(EditBioPage::class, ['bioUlid' => $bio->ulid])
        ->set('title', 'Updated Title')
        ->call('save')
        ->assertDispatched('bio-updated')
        ->assertDispatched('toast');
});

// DeleteBioPage

it('renders delete bio page modal', function () {
    [$user, $ws] = makeUserWithWsForBio();
    app()->instance('current_workspace', $ws);
    $bio = BioPage::factory()->create(['workspace_id' => $ws->id]);

    Livewire::actingAs($user)->test(DeleteBioPage::class, ['bioUlid' => $bio->ulid])
        ->assertOk();
});

it('deletes bio page and dispatches event', function () {
    [$user, $ws] = makeUserWithWsForBio();
    app()->instance('current_workspace', $ws);
    $bio = BioPage::factory()->create(['workspace_id' => $ws->id]);

    Livewire::actingAs($user)->test(DeleteBioPage::class, ['bioUlid' => $bio->ulid])
        ->call('delete')
        ->assertDispatched('bio-deleted')
        ->assertDispatched('toast');

    $this->assertSoftDeleted('bio_pages', ['id' => $bio->id]);
});
