<?php

declare(strict_types=1);

namespace Tests\Integration;

use Tests\Support\DatabaseSeeder;
use Tests\Support\QdbTestCase;

final class TranslationsWriteTest extends QdbTestCase
{
    public function testPutQuestionTranslationAndReadBack(): void
    {
        $token = $this->api()->loginWebAndGetToken(
            DatabaseSeeder::ADMIN_USERNAME,
            DatabaseSeeder::PASSWORD
        )['token'];
        $f = $this->fixture();

        $save = $this->api()->withToken($token, 'PUT', 'translations', [
            'type'         => 'language',
            'languageCode' => 'en',
            'name'         => 'English',
        ]);
        $this->assertApiOk($save);

        $put = $this->api()->withToken($token, 'PUT', 'translations', [
            'type'         => 'question',
            'id'           => $f->questionId,
            'languageCode' => 'en',
            'text'         => 'Consent in English?',
        ]);
        $this->assertApiOk($put);

        $get = $this->api()->withToken($token, 'GET', 'translations', null, [
            'type' => 'question',
            'id'   => $f->questionId,
        ]);
        $this->assertApiOk($get);
        $texts = [];
        foreach ($get['data']['translations'] as $row) {
            $texts[$row['languageCode']] = $row['text'];
        }
        $this->assertArrayHasKey('en', $texts);
        $this->assertSame('Consent in English?', $texts['en']);
    }

    public function testPutAnswerOptionTranslation(): void
    {
        $token = $this->api()->loginWebAndGetToken(
            DatabaseSeeder::ADMIN_USERNAME,
            DatabaseSeeder::PASSWORD
        )['token'];
        $f = $this->fixture();

        $this->api()->withToken($token, 'PUT', 'translations', [
            'type'         => 'language',
            'languageCode' => 'fr',
            'name'         => 'French',
        ]);

        $put = $this->api()->withToken($token, 'PUT', 'translations', [
            'type'         => 'answer_option',
            'id'           => $f->optionId,
            'languageCode' => 'fr',
            'text'         => 'Oui',
        ]);
        $this->assertApiOk($put);

        $get = $this->api()->withToken($token, 'GET', 'translations', null, [
            'type' => 'answer_option',
            'id'   => $f->optionId,
        ]);
        $this->assertApiOk($get);
        $fr = null;
        foreach ($get['data']['translations'] as $row) {
            if ($row['languageCode'] === 'fr') {
                $fr = $row['text'];
            }
        }
        $this->assertSame('Oui', $fr);
    }

    public function testAllTranslationsIncludeGlassSymptomKeys(): void
    {
        $token = $this->api()->loginWebAndGetToken(
            DatabaseSeeder::ADMIN_USERNAME,
            DatabaseSeeder::PASSWORD
        )['token'];
        $f = $this->fixture();

        $res = $this->api()->withToken($token, 'GET', 'translations', null, ['all' => '1']);
        $this->assertApiOk($res);

        $stringKeys = [];
        foreach ($res['data']['entries'] as $entry) {
            if (($entry['type'] ?? '') === 'string') {
                $stringKeys[] = $entry['key'] ?? '';
            }
        }
        $this->assertContains($f->glassSymptomKey, $stringKeys);
        $this->assertContains('fatigue', $stringKeys);
    }
}
