<?php

declare(strict_types=1);

namespace Tests\Unit;

use PHPUnit\Framework\TestCase;

final class CommonHelpersTest extends TestCase
{
    protected function setUp(): void
    {
        require_once dirname(__DIR__, 2) . '/common.php';
    }

    public function testIsStableKey(): void
    {
        $this->assertTrue(qdb_is_stable_key('question_key'));
        $this->assertTrue(qdb_is_stable_key('yes'));
        $this->assertFalse(qdb_is_stable_key('Question Key'));
        $this->assertFalse(qdb_is_stable_key(''));
    }

    public function testNormalizeStableKeyCoercesSpaces(): void
    {
        $this->assertSame('My_Key', qdb_normalize_stable_key('My Key'));
        $this->assertSame('consent_q', qdb_normalize_stable_key('consent_q'));
    }

    public function testValidateStableKeyReturnsNormalized(): void
    {
        $this->assertSame('My_Key', qdb_validate_stable_key('My Key', 'Question key'));
    }

    public function testMessageKeyFromConditionJson(): void
    {
        $json = json_encode(['messageKey' => 'locked_hint'], JSON_THROW_ON_ERROR);
        $this->assertSame('locked_hint', qdb_message_key_from_condition_json($json));
        $this->assertSame('', qdb_message_key_from_condition_json('{}'));
    }

    public function testMergeGlassSymptomJson(): void
    {
        $merged = qdb_merge_glass_symptom_json('{"pain":"mild"}', ['fatigue' => 'severe']);
        $data = json_decode($merged, true, 512, JSON_THROW_ON_ERROR);
        $this->assertSame('mild', $data['pain']);
        $this->assertSame('severe', $data['fatigue']);
    }

    public function testGlassSymptomAnswerValue(): void
    {
        $json = json_encode(['pain' => 'moderate'], JSON_THROW_ON_ERROR);
        $this->assertSame('moderate', qdb_glass_symptom_answer_value($json, 'pain'));
        $this->assertSame('', qdb_glass_symptom_answer_value($json, 'missing'));
    }

    public function testApplyGlassSymptomsConfig(): void
    {
        $config = ['scaleType' => 'glass'];
        $rows = [
            ['key' => 'pain', 'labelDe' => 'Schmerz'],
            ['key' => 'fatigue', 'labelDe' => 'Müdigkeit'],
        ];
        $out = qdb_apply_glass_symptoms_config($config, $rows);
        $this->assertSame(['pain', 'fatigue'], $out['symptoms']);
        $this->assertSame('glass', $out['scaleType']);
    }

    public function testApplyGlassSymptomsConfigClearsWhenEmpty(): void
    {
        $config = ['symptoms' => ['old_key'], 'scaleType' => 'thermometer'];
        $out = qdb_apply_glass_symptoms_config($config, []);
        $this->assertArrayNotHasKey('symptoms', $out);
    }

    public function testParseGlassSymptomsBody(): void
    {
        $body = [
            'glassSymptoms' => [
                ['key' => 'pain', 'labelDe' => 'Schmerz'],
                ['key' => 'invalid key', 'labelDe' => 'x'],
            ],
        ];
        $rows = qdb_parse_glass_symptoms_body($body, []);
        $this->assertSame([
            ['key' => 'pain', 'labelDe' => 'Schmerz'],
            ['key' => 'invalid_key', 'labelDe' => 'x'],
        ], $rows);
    }

    public function testParseGlassSymptomsBodyFromConfigWhenMissing(): void
    {
        $config = ['symptoms' => ['fatigue', 'pain']];
        $rows = qdb_parse_glass_symptoms_body([], $config);
        $this->assertSame([
            ['key' => 'fatigue', 'labelDe' => ''],
            ['key' => 'pain', 'labelDe' => ''],
        ], $rows);
    }
}
