<?php

namespace Tests\Feature\ValueSet;

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

class ValueSetControllerTest extends CIUnitTestCase
{
    use FeatureTestTrait;

    protected function setUp(): void
    {
        parent::setUp();
        ValueSet::clearCache();
    }

    public function testIndexReturnsAllLookupsWithCounts()
    {
        $result = $this->call('get', 'api/valueset');

        $result->assertStatus(200);

        $json = $result->getJSON();
        $data = json_decode($json, true);

        $this->assertEquals('success', $data['status']);
        $this->assertIsArray($data['data']);
        
        // Find specific valuesets in the array
        $valuesets = [];
        foreach ($data['data'] as $item) {
            $valuesets[$item['value']] = $item;
        }
        
        $this->assertArrayHasKey('sex', $valuesets);
        $this->assertArrayHasKey('specimen_type', $valuesets);
        $this->assertArrayHasKey('order_priority', $valuesets);
        $this->assertArrayHasKey('specimen_status', $valuesets);
        
        // Check structure
        $this->assertArrayHasKey('value', $valuesets['sex']);
        $this->assertArrayHasKey('label', $valuesets['sex']);
        $this->assertArrayHasKey('count', $valuesets['sex']);
        $this->assertIsInt($valuesets['sex']['count']);
        $this->assertGreaterThan(0, $valuesets['sex']['count']);
        $this->assertEquals('Sex', $valuesets['sex']['label']);
    }

    public function testShowByNameReturnsSingleLookup()
    {
        $result = $this->call('get', 'api/valueset/sex');

        $result->assertStatus(200);

        $json = $result->getJSON();
        $data = json_decode($json, true);

        $this->assertEquals('success', $data['status']);
        $this->assertIsArray($data['data']);
        $this->assertNotEmpty($data['data']);
        $this->assertArrayHasKey('value', $data['data'][0]);
        $this->assertArrayHasKey('label', $data['data'][0]);
    }

    public function testShowByNameSexReturnsCorrectValues()
    {
        $result = $this->call('get', 'api/valueset/sex');

        $result->assertStatus(200);

        $json = $result->getJSON();
        $data = json_decode($json, true);

        $values = array_column($data['data'], 'value');
        $labels = array_column($data['data'], 'label');

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

    public function testShowByNameInvalidLookupReturns404()
    {
        $result = $this->call('get', 'api/valueset/nonexistent_lookup');

        $result->assertStatus(404);

        $json = $result->getJSON();
        $data = json_decode($json, true);

        $this->assertEquals('error', $data['status']);
    }

    public function testShowByNameOrderPriority()
    {
        $result = $this->call('get', 'api/valueset/order_priority');

        $result->assertStatus(200);

        $json = $result->getJSON();
        $data = json_decode($json, true);

        $this->assertEquals('success', $data['status']);
        $this->assertIsArray($data['data']);

        $labels = array_column($data['data'], 'label');
        $this->assertContains('Stat', $labels);
        $this->assertContains('ASAP', $labels);
        $this->assertContains('Routine', $labels);
    }

    public function testRefreshClearsCache()
    {
        ValueSet::getAll();

        $result = $this->call('post', 'api/valueset/refresh');

        $result->assertStatus(200);

        $json = $result->getJSON();
        $data = json_decode($json, true);

        $this->assertEquals('success', $data['status']);
        $this->assertEquals('Cache cleared', $data['message']);
    }

    public function testShowByNameSpecimenStatus()
    {
        $result = $this->call('get', 'api/valueset/specimen_status');

        $result->assertStatus(200);

        $json = $result->getJSON();
        $data = json_decode($json, true);

        $this->assertEquals('success', $data['status']);
        $this->assertIsArray($data['data']);

        $values = array_column($data['data'], 'value');
        $labels = array_column($data['data'], 'label');

        $this->assertContains('STC', $values);
        $this->assertContains('SCtd', $values);
        $this->assertContains('To be collected', $labels);
        $this->assertContains('Collected', $labels);
    }
}
