<?php

namespace Tests\Feature\Patients;

use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\FeatureTestTrait;
use Faker\Factory;

class PatientCheckTest extends CIUnitTestCase
{
    use FeatureTestTrait;

    protected $endpoint = 'api/patient/check';

    public function testCheckPatientIDExists()
    {
        $result = $this->withHeaders(['Accept' => 'application/json'])->call('get', $this->endpoint, [
            'PatientID' => 'SMAJ1',
        ]);

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

        $this->assertSame('success', $data['status']);
        $this->assertSame(false, $data['data']);
    }

    public function testCheckPatientIDWithHyphenExists()
    {
        $result = $this->withHeaders(['Accept' => 'application/json'])->call('get', $this->endpoint, [
            'PatientID' => 'SMAJ-1',
        ]);

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

        $this->assertSame('success', $data['status']);
        $this->assertIsBool($data['data']);
    }

    public function testCheckPatientIDNotExists()
    {
        $faker = Factory::create();

        $result = $this->withHeaders(['Accept' => 'application/json'])->call('get', $this->endpoint, [
            'PatientID' => 'NONEXISTENT-' . $faker->numberBetween(100000, 999999),
        ]);

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

        $this->assertSame('success', $data['status']);
        $this->assertTrue($data['data']);
    }

    public function testCheckEmailAddressExists()
    {
        $result = $this->withHeaders(['Accept' => 'application/json'])->call('get', $this->endpoint, [
            'EmailAddress' => 'dummy1@test.com',
        ]);

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

        $this->assertSame('success', $data['status']);
        $this->assertIsBool($data['data']);
    }

    public function testCheckEmailAddressMatchesSecondaryColumn()
    {
        $result = $this->withHeaders(['Accept' => 'application/json'])->call('get', $this->endpoint, [
            'EmailAddress' => 'dummy1@test.com',
        ]);

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

        $this->assertSame('success', $data['status']);
        $this->assertIsBool($data['data']);
    }

    public function testCheckPhoneExists()
    {
        $result = $this->withHeaders(['Accept' => 'application/json'])->call('get', $this->endpoint, [
            'Phone' => '092029',
        ]);

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

        $this->assertSame('success', $data['status']);
        $this->assertIsBool($data['data']);
    }

    public function testCheckWithoutParams()
    {
        $result = $this->withHeaders(['Accept' => 'application/json'])->call('get', $this->endpoint);

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

        $this->assertSame('error', $data['status']);
        $this->assertSame('PatientID, EmailAddress, or Phone parameter is required.', $data['message']);
    }

    public function testCheckResponseStructure()
    {
        $result = $this->withHeaders(['Accept' => 'application/json'])->call('get', $this->endpoint, [
            'PatientID' => 'TEST123',
        ]);

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

        $this->assertArrayHasKey('status', $data);
        $this->assertArrayHasKey('message', $data);
        $this->assertArrayHasKey('data', $data);
    }
}
