<?php

declare(strict_types=1);

use App\Filament\Pages\ChooseWorkspace;
use App\Filament\Pages\TenantDashboard;
use App\Models\Tenant;
use App\Models\TenantMembership;
use App\Models\User;
use App\Models\Workspace;
use App\Models\WorkspaceMembership;
use App\Support\Workspaces\WorkspaceContext;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;

uses(RefreshDatabase::class);

it('redirects to the workspace managed-tenants landing after selecting a workspace with no tenants', function (): void {
    $user = User::factory()->create();

    $workspace = Workspace::factory()->create();
    WorkspaceMembership::factory()->create([
        'workspace_id' => $workspace->getKey(),
        'user_id' => $user->getKey(),
        'role' => 'owner',
    ]);

    Livewire::actingAs($user)
        ->test(ChooseWorkspace::class)
        ->call('selectWorkspace', $workspace->getKey())
        ->assertRedirect(route('admin.workspace.managed-tenants.index', ['workspace' => $workspace->slug ?? $workspace->getKey()]));
});

it('redirects to the tenant dashboard after selecting a workspace with exactly one tenant', function (): void {
    $user = User::factory()->create();

    $workspace = Workspace::factory()->create();
    WorkspaceMembership::factory()->create([
        'workspace_id' => $workspace->getKey(),
        'user_id' => $user->getKey(),
        'role' => 'owner',
    ]);

    $tenant = Tenant::factory()->create([
        'status' => 'active',
        'workspace_id' => $workspace->getKey(),
    ]);

    TenantMembership::query()->create([
        'tenant_id' => $tenant->getKey(),
        'user_id' => $user->getKey(),
        'role' => 'owner',
        'source' => 'manual',
        'source_ref' => null,
        'created_by_user_id' => null,
    ]);

    Livewire::actingAs($user)
        ->test(ChooseWorkspace::class)
        ->call('selectWorkspace', $workspace->getKey())
        ->assertRedirect(TenantDashboard::getUrl(tenant: $tenant));
});

it('redirects to choose-tenant after selecting a workspace with multiple tenants', function (): void {
    $user = User::factory()->create();

    $workspace = Workspace::factory()->create();
    WorkspaceMembership::factory()->create([
        'workspace_id' => $workspace->getKey(),
        'user_id' => $user->getKey(),
        'role' => 'owner',
    ]);

    $tenants = Tenant::factory()->count(2)->create([
        'status' => 'active',
        'workspace_id' => $workspace->getKey(),
    ]);

    foreach ($tenants as $tenant) {
        TenantMembership::query()->create([
            'tenant_id' => $tenant->getKey(),
            'user_id' => $user->getKey(),
            'role' => 'owner',
            'source' => 'manual',
            'source_ref' => null,
            'created_by_user_id' => null,
        ]);
    }

    Livewire::actingAs($user)
        ->test(ChooseWorkspace::class)
        ->call('selectWorkspace', $workspace->getKey())
        ->assertRedirect('/admin/choose-tenant');
});

it('prefers the stored intended url after selecting a workspace', function (): void {
    $user = User::factory()->create();

    $workspace = Workspace::factory()->create();
    WorkspaceMembership::factory()->create([
        'workspace_id' => $workspace->getKey(),
        'user_id' => $user->getKey(),
        'role' => 'owner',
    ]);

    session()->put(WorkspaceContext::INTENDED_URL_SESSION_KEY, '/admin/operations');

    Livewire::actingAs($user)
        ->test(ChooseWorkspace::class)
        ->call('selectWorkspace', $workspace->getKey())
        ->assertRedirect('/admin/operations');
});
