<?php

declare(strict_types=1);

use App\Models\AuditLog;
use App\Services\Localization\LocaleResolver;
use App\Services\Settings\SettingsWriter;
use App\Support\Audit\AuditActionId;
use Illuminate\Support\Facades\App;

it('keeps audit action identifiers and machine values invariant while UI locale is German', function (): void {
    [$workspace, $user] = localizationWorkspaceMember();

    App::setLocale('de');

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

    $audit = AuditLog::query()->latest('id')->first();

    expect($audit)->not->toBeNull()
        ->and($audit->action)->toBe(AuditActionId::WorkspaceSettingUpdated->value)
        ->and(data_get($audit->metadata, 'domain'))->toBe(LocaleResolver::SETTING_DOMAIN)
        ->and(data_get($audit->metadata, 'key'))->toBe(LocaleResolver::SETTING_DEFAULT_LOCALE)
        ->and(data_get($audit->metadata, 'after_value'))->toBe('de');
});
