<?php

namespace Tests\Unit;

use App\Services\BladeUiTranslationService;
use PHPUnit\Framework\TestCase;

class BladeUiTranslationServiceTest extends TestCase
{
    public function test_it_discovers_and_compiles_safe_static_ui_text(): void
    {
        $service = new BladeUiTranslationService();
        $blade = <<<'BLADE'
@section('title', 'Calendar')
<x-ui.page-toolbar title="Daily Reports" description="Manage confirmed reports" :breadcrumbs="[['label'=>'Dashboard','url'=>route('dashboard')]]" />
<div>
    <h2>Report recipients</h2>
    <input placeholder="Manager name">
    <button>Add</button>
    <span>{{ $customer->name }}</span>
</div>
<script>const label = 'Do not translate';</script>
BLADE;

        $catalog = $service->extract($blade);

        $this->assertContains('Calendar', $catalog);
        $this->assertContains('Daily Reports', $catalog);
        $this->assertContains('Manage confirmed reports', $catalog);
        $this->assertContains('Dashboard', $catalog);
        $this->assertContains('Report recipients', $catalog);
        $this->assertContains('Manager name', $catalog);
        $this->assertContains('Add', $catalog);
        $this->assertNotContains('Do not translate', $catalog);

        $compiled = $service->compile($blade);
        $reportKey = $service->key('Report recipients');
        $placeholderKey = $service->key('Manager name');

        $this->assertStringContainsString("->ui('{$reportKey}', 'Report recipients')", $compiled);
        $this->assertStringContainsString("placeholder=\"{{ app(\\App\\Services\\LocalizationService::class)->ui('{$placeholderKey}', 'Manager name') }}\"", $compiled);
        $this->assertStringContainsString('{{ $customer->name }}', $compiled);
        $this->assertStringContainsString("const label = 'Do not translate';", $compiled);
    }

    public function test_same_text_always_uses_same_translation_key(): void
    {
        $service = new BladeUiTranslationService();

        $this->assertSame($service->key('  Save   changes '), $service->key('Save changes'));
    }
}
