<?php

use App\Enums\WorkspaceUserRole;
use App\Models\Client;
use App\Models\User;
use App\Models\Workspace;
use Spatie\Activitylog\Models\Activity;

function setupSwitchTestUser(string $role, int $workspaceCount = 2): array
{
    $user = User::factory()->create();
    $workspaces = [];

    for ($i = 0; $i < $workspaceCount; $i++) {
        $workspace = Workspace::factory()->create();
        $workspace->users()->attach($user->id, [
            'role' => $role,
            'permissions' => [],
        ]);
        $workspaces[] = $workspace;
    }

    return [$user, $workspaces];
}

test('owner with multiple workspaces can switch workspace', function () {
    [$user, $workspaces] = setupSwitchTestUser(WorkspaceUserRole::Owner);

    $response = $this->actingAs($user)
        ->withSession(['current_workspace_id' => $workspaces[0]->id])
        ->post(route('workspace.switch'), [
            'workspace_id' => $workspaces[1]->id,
        ]);

    $response->assertRedirect(route('dashboard'));

    expect(session('current_workspace_id'))->toBe($workspaces[1]->id);
});

test('workspace switching logs activity with previous and new workspace ids', function () {
    [$user, $workspaces] = setupSwitchTestUser(WorkspaceUserRole::Owner);

    $this->actingAs($user)
        ->withSession(['current_workspace_id' => $workspaces[0]->id])
        ->post(route('workspace.switch'), [
            'workspace_id' => $workspaces[1]->id,
        ]);

    $log = Activity::latest('id')->first();

    expect($log->description)->toBe('Switched workspace')
        ->and($log->causer_id)->toBe($user->id)
        ->and($log->properties['previous_workspace_id'])->toBe($workspaces[0]->id)
        ->and($log->properties['new_workspace_id'])->toBe($workspaces[1]->id);
});

test('user cannot switch to a workspace they do not belong to', function () {
    [$user, $workspaces] = setupSwitchTestUser(WorkspaceUserRole::Worker, 1);
    $otherWorkspace = Workspace::factory()->create();

    $response = $this->actingAs($user)
        ->withSession(['current_workspace_id' => $workspaces[0]->id])
        ->post(route('workspace.switch'), [
            'workspace_id' => $otherWorkspace->id,
        ]);

    $response->assertRedirect(route('dashboard'));

    expect(session('current_workspace_id'))->toBe($workspaces[0]->id)
        ->and(Activity::where('description', 'Switched workspace')->count())->toBe(0);
});

test('switching updates auth shared props on next page load', function () {
    [$user, $workspaces] = setupSwitchTestUser(WorkspaceUserRole::Owner);

    $this->actingAs($user)
        ->withSession(['current_workspace_id' => $workspaces[0]->id])
        ->post(route('workspace.switch'), [
            'workspace_id' => $workspaces[1]->id,
        ]);

    $response = $this->actingAs($user)->get(route('dashboard'));

    $response->assertInertia(fn ($page) => $page
        ->where('auth.currentWorkspace.id', $workspaces[1]->id)
        ->where('auth.workspaceRole', 'owner')
        ->has('auth.workspaceSwitchUrl')
    );
});

test('user with single workspace can post switch with same workspace id without error', function () {
    [$user, $workspaces] = setupSwitchTestUser(WorkspaceUserRole::Worker, 1);

    $response = $this->actingAs($user)
        ->withSession(['current_workspace_id' => $workspaces[0]->id])
        ->post(route('workspace.switch'), [
            'workspace_id' => $workspaces[0]->id,
        ]);

    $response->assertRedirect(route('dashboard'));

    expect(session('current_workspace_id'))->toBe($workspaces[0]->id);
});

test('cross-workspace isolation: after switching, data queries return only new workspace data', function () {
    [$user, $workspaces] = setupSwitchTestUser(WorkspaceUserRole::Owner);

    $clientA = Client::factory()->create(['workspace_id' => $workspaces[0]->id]);
    $clientB = Client::factory()->create(['workspace_id' => $workspaces[1]->id]);

    $this->actingAs($user)
        ->withSession(['current_workspace_id' => $workspaces[0]->id])
        ->post(route('workspace.switch'), [
            'workspace_id' => $workspaces[1]->id,
        ]);

    $response = $this->actingAs($user)->get(route('clients.index'));

    $response->assertInertia(fn ($page) => $page
        ->has('clients.data', 1)
        ->where('clients.data.0.id', $clientB->id)
    );
});
