<?php

namespace Tests\Unit\ValueSet;

use CodeIgniter\Test\CIUnitTestCase;
use App\Libraries\ValueSet;

class ValueSetTest extends CIUnitTestCase
{
    protected function setUp(): void
    {
        parent::setUp();
        ValueSet::clearCache();
    }

    public function testGetPatientSexReturnsFormattedArray()
    {
        $result = ValueSet::get('sex');
        $this->assertIsArray($result);
        $this->assertNotEmpty($result);
        $this->assertArrayHasKey('value', $result[0]);
        $this->assertArrayHasKey('label', $result[0]);
    }

    public function testGetPatientSexContainsExpectedValues()
    {
        $result = ValueSet::get('sex');
        $values = array_column($result, 'value');
        $labels = array_column($result, 'label');

        $this->assertContains('F', $values);
        $this->assertContains('M', $values);
        $this->assertContains('U', $values);
        $this->assertContains('Female', $labels);
        $this->assertContains('Male', $labels);
        $this->assertContains('Unknown', $labels);
    }

    public function testGetRawReturnsArrayOfKeyValuePairs()
    {
        $result = ValueSet::getRaw('sex');
        $this->assertIsArray($result);
        $this->assertNotEmpty($result);
        $this->assertArrayHasKey('key', $result[0]);
        $this->assertArrayHasKey('value', $result[0]);
    }

    public function testGetRawPatientSexContainsExpectedData()
    {
        $result = ValueSet::getRaw('sex');
        $keys = array_column($result, 'key');
        $values = array_column($result, 'value');

        $this->assertContains('F', $keys);
        $this->assertContains('M', $keys);
        $this->assertContains('Female', $values);
        $this->assertContains('Male', $values);
    }

    public function testGetLabelConvertsCodeToLabel()
    {
        $this->assertEquals('Female', ValueSet::getLabel('sex', 'F'));
        $this->assertEquals('Male', ValueSet::getLabel('sex', 'M'));
        $this->assertEquals('Unknown', ValueSet::getLabel('sex', 'U'));
    }

    public function testGetLabelForOrderPriority()
    {
        $this->assertEquals('Stat', ValueSet::getLabel('order_priority', 'S'));
        $this->assertEquals('ASAP', ValueSet::getLabel('order_priority', 'A'));
        $this->assertEquals('Routine', ValueSet::getLabel('order_priority', 'R'));
        $this->assertEquals('Preop', ValueSet::getLabel('order_priority', 'P'));
    }

    public function testGetLabelReturnsNullForInvalidKey()
    {
        $this->assertNull(ValueSet::getLabel('sex', '99'));
        $this->assertNull(ValueSet::getLabel('sex', 'invalid'));
    }

    public function testGetLabelReturnsNullForInvalidLookup()
    {
        $this->assertNull(ValueSet::getLabel('nonexistent_lookup', '1'));
    }

    public function testGetReturnsNullForInvalidLookup()
    {
        $result = ValueSet::get('nonexistent_lookup');
        $this->assertNull($result);
    }

    public function testGetAllReturnsMultipleLookups()
    {
        $result = ValueSet::getAll();
        $this->assertIsArray($result);
        $this->assertArrayHasKey('sex', $result);
        $this->assertArrayHasKey('specimen_type', $result);
        $this->assertArrayHasKey('order_priority', $result);
        $this->assertArrayHasKey('specimen_status', $result);
    }

    public function testGetAllContainsValuesKey()
    {
        $result = ValueSet::getAll();
        $this->assertIsArray($result['sex']['values']);
        $this->assertNotEmpty($result['sex']['values']);
    }

    public function testGetAllContainsMetadata()
    {
        $result = ValueSet::getAll();
        $this->assertArrayHasKey('name', $result['sex']);
        $this->assertArrayHasKey('VSName', $result['sex']);
        $this->assertArrayHasKey('VCategory', $result['sex']);
        $this->assertArrayHasKey('values', $result['sex']);
    }

    public function testGetPatientSex()
    {
        $result = ValueSet::get('sex');
        $this->assertEquals('F', $result[0]['value']);
        $this->assertEquals('Female', $result[0]['label']);
        $this->assertEquals('M', $result[1]['value']);
        $this->assertEquals('Male', $result[1]['label']);
    }

    public function testGetSpecimenStatus()
    {
        $result = ValueSet::getRaw('specimen_status');
        $keys = array_column($result, 'key');
        $values = array_column($result, 'value');

        $this->assertContains('STC', $keys);
        $this->assertContains('SCtd', $keys);
        $this->assertContains('STran', $keys);
        $this->assertContains('SArrv', $keys);
        $this->assertContains('SRejc', $keys);
        $this->assertContains('SRcvd', $keys);

        $statusMap = array_combine($keys, $values);
        $this->assertEquals('To be collected', $statusMap['STC']);
        $this->assertEquals('Collected', $statusMap['SCtd']);
        $this->assertEquals('In-transport', $statusMap['STran']);
    }

    public function testGetOrderPriority()
    {
        $result = ValueSet::getRaw('order_priority');
        $keys = array_column($result, 'key');
        $values = array_column($result, 'value');

        $this->assertContains('S', $keys);
        $this->assertContains('A', $keys);
        $this->assertContains('R', $keys);

        $priorityMap = array_combine($keys, $values);
        $this->assertEquals('Stat', $priorityMap['S']);
        $this->assertEquals('ASAP', $priorityMap['A']);
        $this->assertEquals('Routine', $priorityMap['R']);
        $this->assertEquals('Preop', $priorityMap['P']);
    }

    public function testGetSpecimenType()
    {
        $result = ValueSet::getRaw('specimen_type');
        $keys = array_column($result, 'key');
        $values = array_column($result, 'value');

        $typeMap = array_combine($keys, $values);
        $this->assertEquals('Whole blood', $typeMap['BLD']);
        $this->assertEquals('Serum', $typeMap['SER']);
        $this->assertEquals('Plasma', $typeMap['PLAS']);
        $this->assertEquals('Urine', $typeMap['UR']);
    }

    public function testGetMaritalStatus()
    {
        $result = ValueSet::getRaw('marital_status');
        $keys = array_column($result, 'key');
        $values = array_column($result, 'value');

        $statusMap = array_combine($keys, $values);
        $this->assertEquals('Separated', $statusMap['A']);
        $this->assertEquals('Divorced', $statusMap['D']);
        $this->assertEquals('Married', $statusMap['M']);
        $this->assertEquals('Single', $statusMap['S']);
        $this->assertEquals('Widowed', $statusMap['W']);
    }

    public function testGetPatientReligion()
    {
        $result = ValueSet::getRaw('religion');
        $keys = array_column($result, 'key');
        $values = array_column($result, 'value');

        $religionMap = array_combine($keys, $values);
        $this->assertEquals('Islam', $religionMap['ISLAM']);
        $this->assertEquals('Kristen', $religionMap['KRSTN']);
        $this->assertEquals('Katolik', $religionMap['KTLIK']);
        $this->assertEquals('Hindu', $religionMap['HINDU']);
        $this->assertEquals('Budha', $religionMap['BUDHA']);
    }

    public function testGetResultType()
    {
        $result = ValueSet::getRaw('result_type');
        $keys = array_column($result, 'key');
        $values = array_column($result, 'value');

        $typeMap = array_combine($keys, $values);
        $this->assertEquals('Numeric', $typeMap['NMRIC']);
        $this->assertEquals('Range', $typeMap['RANGE']);
        $this->assertEquals('Text', $typeMap['TEXT']);
        $this->assertEquals('Value set', $typeMap['VSET']);
    }

    public function testClearCacheDoesNotThrowError()
    {
        ValueSet::getAll();
        $result = ValueSet::clearCache();
        $this->assertTrue($result);
    }

    public function testGetOrderPriorityFromAlias()
    {
        $result = ValueSet::getRaw('priority');
        $keys = array_column($result, 'key');
        $values = array_column($result, 'value');

        $priorityMap = array_combine($keys, $values);
        $this->assertEquals('Stat', $priorityMap['S']);
        $this->assertEquals('Routine', $priorityMap['R']);
    }

    public function testGetTestStatus()
    {
        $result = ValueSet::getRaw('test_status');
        $keys = array_column($result, 'key');
        $values = array_column($result, 'value');

        $statusMap = array_combine($keys, $values);
        $this->assertEquals('Waiting for Results', $statusMap['PENDING']);
        $this->assertEquals('Analyzing', $statusMap['IN_PROCESS']);
        $this->assertEquals('Verified & Signed', $statusMap['VERIFIED']);
    }

    public function testGetOrderRequestStatus()
    {
        $result = ValueSet::getRaw('request_status');
        $keys = array_column($result, 'key');
        $values = array_column($result, 'value');

        $statusMap = array_combine($keys, $values);
        $this->assertEquals('To be collected', $statusMap['STC']);
        $this->assertEquals('Collected', $statusMap['SCtd']);
    }

    public function testGetResultResultStatus()
    {
        $result = ValueSet::getRaw('result_status');
        $keys = array_column($result, 'key');
        $values = array_column($result, 'value');

        $statusMap = array_combine($keys, $values);
        $this->assertEquals('Preliminary', $statusMap['PRELIMINARY']);
        $this->assertEquals('Final', $statusMap['FINAL']);
        $this->assertEquals('Corrected', $statusMap['CORRECTED']);
    }

    public function testGetReturnsFormattedValues()
    {
        $result = ValueSet::get('sex');
        $this->assertEquals('F', $result[0]['value']);
        $this->assertEquals('Female', $result[0]['label']);
        $this->assertEquals('M', $result[1]['value']);
        $this->assertEquals('Male', $result[1]['label']);
    }

    public function testGetWithSpecialCharactersInKey()
    {
        $result = ValueSet::get('result_unit');
        $values = array_column($result, 'value');
        $this->assertContains('g/dL', $values);
        $this->assertContains('mg/dL', $values);
        $this->assertContains('x106/mL', $values);
    }

    public function testGetMathSigns()
    {
        $result = ValueSet::getRaw('math_sign');
        $keys = array_column($result, 'key');
        $values = array_column($result, 'value');

        $signMap = array_combine($keys, $values);
        $this->assertEquals('Equal', $signMap['=']);
        $this->assertEquals('Less than', $signMap['<']);
        $this->assertEquals('Greater than', $signMap['>']);
        $this->assertEquals('Less than or equal to', $signMap['<=']);
        $this->assertEquals('Greater than or equal to', $signMap['>=']);
    }

    public function testGetContainerCapColor()
    {
        $result = ValueSet::getRaw('container_cap_color');
        $keys = array_column($result, 'key');
        $values = array_column($result, 'value');

        $colorMap = array_combine($keys, $values);
        $this->assertEquals('Purple', $colorMap['PRPL']);
        $this->assertEquals('Red', $colorMap['RED']);
        $this->assertEquals('Yellow', $colorMap['YLLW']);
        $this->assertEquals('Green', $colorMap['GRN']);
    }

    public function testGetOrganizationSiteType()
    {
        $result = ValueSet::getRaw('site_type');
        $keys = array_column($result, 'key');
        $values = array_column($result, 'value');

        $siteMap = array_combine($keys, $values);
        $this->assertEquals('Government Hospital', $siteMap['GH']);
        $this->assertEquals('Private Hospital', $siteMap['PH']);
        $this->assertEquals('Government Lab', $siteMap['GL']);
        $this->assertEquals('Private Lab', $siteMap['PL']);
    }

    public function testGetPatEntityType()
    {
        $result = ValueSet::getRaw('entity_type');
        $keys = array_column($result, 'key');
        $values = array_column($result, 'value');

        $entityMap = array_combine($keys, $values);
        $this->assertEquals('HIS', $entityMap['HIS']);
        $this->assertEquals('Site', $entityMap['SITE']);
        $this->assertEquals('Workstation', $entityMap['WST']);
        $this->assertEquals('Equipment/Instrument', $entityMap['INST']);
    }

    public function testGetTestType()
    {
        $result = ValueSet::get('test_type');
        $this->assertIsArray($result);
        $this->assertNotEmpty($result);
        
        $values = array_column($result, 'value');
        $this->assertContains('TEST', $values);
        $this->assertContains('PARAM', $values);
        $this->assertContains('CALC', $values);
        $this->assertContains('GROUP', $values);
        $this->assertContains('TITLE', $values);
    }

    public function testTransformLabels()
    {
        $data = [
            ['Gender' => 'F', 'Country' => 'IDN'],
            ['Gender' => 'M', 'Country' => 'USA']
        ];

        $result = ValueSet::transformLabels($data, [
            'Gender' => 'sex',
            'Country' => 'country'
        ]);

        $this->assertEquals('F', $result[0]['Gender']);
        $this->assertEquals('Female', $result[0]['GenderLabel']);
        $this->assertEquals('M', $result[1]['Gender']);
        $this->assertEquals('Male', $result[1]['GenderLabel']);
        $this->assertEquals('USA', $result[1]['Country']);
        $this->assertEquals('United States of America', $result[1]['CountryLabel']);
    }

    public function testGetOptions()
    {
        $result = ValueSet::getOptions('sex');
        $this->assertIsArray($result);
        $this->assertNotEmpty($result);
        $this->assertArrayHasKey('key', $result[0]);
        $this->assertArrayHasKey('value', $result[0]);
    }
}

