<?php

namespace Tests\Feature\Patients;

use CodeIgniter\Test\FeatureTestTrait;
use CodeIgniter\Test\CIUnitTestCase;

class PatientIndexTest extends CIUnitTestCase
{
    use FeatureTestTrait;

    protected $endpoint = 'api/patient';

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

    /**
     *   Case 1: tanpa parameter, harus 200 dan status success - Passed
     */
    public function testIndexWithoutParams() {
        $result = $this->call('get', $this->endpoint);

        $result->assertStatus(200);
        $result->assertJSONFragment([
            'status' => 'success',
        ]);
    }

    /**
     *   Case 2: parameter Name yang tidak ada → return kosong [] - Passed
     */
    public function testIndexWithWrongNameParam() {
        $result = $this->call('get', $this->endpoint, [
            'Name' => 'TidakAdaNama'
        ]);

        $result->assertStatus(200);
        $result->assertJSONFragment([
            'status' => 'success',
            'data'   => []
        ]);
    }

    /**
     *   Case 3: parameter Name benar → return array tidak kosong - Passed
     */
    public function testIndexWithCorrectNameParam() {
        // Sesuaikan dengan data di database test Anda
        $result = $this->call('get', $this->endpoint, [
            'Name' => 'Jane'
        ]);

        $result->assertStatus(200);

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

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

    /**
     *   Case 4: parameter InternalPID → return data sesuai ID - Passed
     */
    public function testIndexWithInternalPID() {
        $result = $this->call('get', $this->endpoint, [
            'InternalPID' => 1
        ]);

        $result->assertStatus(200);

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

        $this->assertEquals('success', $data['status']);
        if (!empty($data['data'])) {
            $this->assertEquals(1, $data['data'][0]['InternalPID']);
        }
    }

    /**
     *   Case 5: parameter PatientID → return data sesuai PatientID - Passed
     */
    public function testIndexWithPatientID() {
        $result = $this->call('get', $this->endpoint, [
            'PatientID' => '123'
        ]);

        $result->assertStatus(200);

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

        $this->assertEquals('success', $data['status']);
        if (!empty($data['data'])) {
            $this->assertStringContainsString('123', $data['data'][0]['PatientID']);
        }
    }

    /**
     *   Case 6: parameter Birthdate → return data sesuai tanggal - Passed
     */
    public function testIndexWithBirthdate() {
        $result = $this->call('get', $this->endpoint, [
            'Birthdate' => '2000-01-01'
        ]);

        $result->assertStatus(200);

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

        $this->assertEquals('success', $data['status']);
        if (!empty($data['data'])) {
            $this->assertEquals('2000-01-01', $data['data'][0]['Birthdate']);
        }
    }

    /**
     *   Case 7: Simulasi server error (optional, jika bisa trigger) - Passed
     */
    public function testIndexServerErrorSimulation() {
        // Misalnya panggil dengan param aneh untuk trigger exception
        $result = $this->call('get', $this->endpoint, [
            'InternalPID' => "' OR 1=2 --"
        ]);

        // Boleh assert 200 (jika sanitasi bagus) atau 500 (kalau exception)
        $this->assertContains($result->getStatusCode(), [200, 500, null]);
    }

}
