<?php

declare(strict_types=1);

use App\Services\Localization\LocaleResolver;
use App\Support\Workspaces\WorkspaceContext;

it('renders the admin auth surface in the explicit locale override', function (): void {
    $this->withSession([LocaleResolver::SESSION_OVERRIDE_KEY => 'de'])
        ->get('/admin/login')
        ->assertSuccessful()
        ->assertSee('Mit Microsoft anmelden')
        ->assertSee('Admin-Zugriff erfordert eine Umgebungsmitgliedschaft');
});

it('keeps system plane resolution independent from user and workspace preferences', function (): void {
    [$workspace, $user] = localizationWorkspaceMember();

    $user->forceFill(['preferred_locale' => 'de'])->save();
    session()->forget(LocaleResolver::SESSION_OVERRIDE_KEY);

    $this->actingAs($user)
        ->withSession([
            WorkspaceContext::SESSION_KEY => (int) $workspace->getKey(),
            LocaleResolver::SESSION_OVERRIDE_KEY => null,
        ])
        ->getJson('/localization/context?plane=system')
        ->assertSuccessful()
        ->assertJsonPath('locale', 'en')
        ->assertJsonPath('source', LocaleResolver::SOURCE_SYSTEM_DEFAULT)
        ->assertJsonPath('user_preference_locale', null)
        ->assertJsonPath('workspace_default_locale', null);

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