<?php

declare(strict_types=1);

use App\Filament\Pages\Reviews\CustomerReviewWorkspace;
use App\Models\ManagedEnvironment;
use App\Services\Localization\LocaleResolver;
use App\Services\Settings\SettingsWriter;
use App\Support\Workspaces\WorkspaceContext;

it('allows users to save and clear a personal locale preference over workspace default', function (): void {
    [$workspace, $user] = localizationWorkspaceMember();

    app(SettingsWriter::class)->updateWorkspaceSetting(
        actor: $user,
        workspace: $workspace,
        domain: LocaleResolver::SETTING_DOMAIN,
        key: LocaleResolver::SETTING_DEFAULT_LOCALE,
        value: 'de',
    );

    $this->actingAs($user)
        ->withSession([WorkspaceContext::SESSION_KEY => (int) $workspace->getKey()])
        ->getJson('/localization/context')
        ->assertSuccessful()
        ->assertJsonPath('locale', 'de')
        ->assertJsonPath('source', LocaleResolver::SOURCE_WORKSPACE_DEFAULT);

    $this->actingAs($user)
        ->withSession([WorkspaceContext::SESSION_KEY => (int) $workspace->getKey()])
        ->post(route('localization.preference.update'), ['preferred_locale' => 'en'])
        ->assertRedirect();

    expect($user->refresh()->preferred_locale)->toBe('en');

    $this->actingAs($user)
        ->withSession([WorkspaceContext::SESSION_KEY => (int) $workspace->getKey()])
        ->getJson('/localization/context')
        ->assertSuccessful()
        ->assertJsonPath('locale', 'en')
        ->assertJsonPath('source', LocaleResolver::SOURCE_USER_PREFERENCE);

    $this->actingAs($user)
        ->withSession([WorkspaceContext::SESSION_KEY => (int) $workspace->getKey()])
        ->post(route('localization.preference.update'), ['preferred_locale' => ''])
        ->assertRedirect();

    expect($user->refresh()->preferred_locale)->toBeNull();

    $this->actingAs($user)
        ->withSession([WorkspaceContext::SESSION_KEY => (int) $workspace->getKey()])
        ->getJson('/localization/context')
        ->assertSuccessful()
        ->assertJsonPath('locale', 'de')
        ->assertJsonPath('source', LocaleResolver::SOURCE_WORKSPACE_DEFAULT);
});

it('allows temporary overrides to win until cleared', function (): void {
    [$workspace, $user] = localizationWorkspaceMember();

    $user->forceFill(['preferred_locale' => 'en'])->save();

    $this->actingAs($user)
        ->withSession([WorkspaceContext::SESSION_KEY => (int) $workspace->getKey()])
        ->post(route('localization.override.update'), ['locale' => 'de'])
        ->assertRedirect();

    expect(session(LocaleResolver::SESSION_OVERRIDE_KEY))->toBe('de');

    $this->actingAs($user)
        ->withSession([
            WorkspaceContext::SESSION_KEY => (int) $workspace->getKey(),
            LocaleResolver::SESSION_OVERRIDE_KEY => 'de',
        ])
        ->getJson('/localization/context')
        ->assertSuccessful()
        ->assertJsonPath('locale', 'de')
        ->assertJsonPath('source', LocaleResolver::SOURCE_EXPLICIT_OVERRIDE);

    $this->actingAs($user)
        ->withSession([
            WorkspaceContext::SESSION_KEY => (int) $workspace->getKey(),
            LocaleResolver::SESSION_OVERRIDE_KEY => 'de',
        ])
        ->delete(route('localization.override.clear'))
        ->assertRedirect();

    expect(session(LocaleResolver::SESSION_OVERRIDE_KEY))->toBeNull();
});

it('returns to the customer review workspace filter when the locale override changes', function (): void {
    $tenant = ManagedEnvironment::factory()->create();
    [$user, $tenant] = createUserWithTenant(tenant: $tenant, role: 'readonly');

    $workspaceUrl = CustomerReviewWorkspace::environmentFilterUrl($tenant);

    $this->actingAs($user)
        ->withSession([WorkspaceContext::SESSION_KEY => (int) $tenant->workspace_id])
        ->from($workspaceUrl)
        ->post(route('localization.override.update'), ['locale' => 'de'])
        ->assertRedirect($workspaceUrl);
});
