<?php

namespace Tests\Feature\PatVisit;

use CodeIgniter\Test\FeatureTestTrait;
use CodeIgniter\Test\CIUnitTestCase;
use Tests\Support\Traits\CreatesPatients;

class PatVisitCreateTest extends CIUnitTestCase
{
    use FeatureTestTrait;
    use CreatesPatients;

    protected $endpoint = 'api/patvisit';

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

    /**
     * Test: Create patient visit with valid data
     */
    public function testCreatePatientVisitSuccess()
    {
        $payload = [
            "InternalPID"=> $this->createTestPatient(),
            "EpisodeID"=> null,
            "PatDiag"=> [
                "DiagCode"=> null,
                "Diagnosis"=> null
            ],
            "PatVisitADT"=> [
                "ADTCode"=> "A01",
                "LocationID"=> "1",
                "AttDoc"=> "1",
                "RefDoc"=> "1",
                "AdmDoc"=> "1",
                "CnsDoc"=> "1"
            ],
            "PatAtt"=> []
        ];

        $response = $this->withBodyFormat('json')->call('post', $this->endpoint, $payload);

        // Pastikan status 201 (created)
        $response->assertStatus(201);

        // Pastikan JSON mengandung status success dan message yang benar
        $response->assertJSONFragment([
            'status'  => 'success',
            'message' => 'Data created successfully'
        ]);

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

        // Pastikan struktur data yang dikembalikan benar
        $this->assertArrayHasKey('data', $json);
        $this->assertArrayHasKey('PVID', $json['data']);
        $this->assertArrayHasKey('InternalPVID', $json['data']);
    }

    /**
     * Test: Create patient visit with invalid (empty) data - missing InternalPID
     */
    public function testCreatePatientVisitInvalidInput()
    {
        $payload = [];
        // Kirim data kosong
        $response = $this->withBodyFormat('json')->call('post', $this->endpoint, $payload);

        $response->assertStatus(400);
        $response->assertJSONFragment([
            'status'  => 'error',
            'message' => 'InternalPID is required and must be numeric'
        ]);
    }

    /**
     * Test: Create patient visit with non-existent patient
     */
    public function testCreatePatientVisitPatientNotFound()
    {
        $payload = [
            'InternalPID' => 999999, // Non-existent patient
            'EpisodeID' => 'EPI001'
        ];

        $response = $this->withBodyFormat('json')->call('post', $this->endpoint, $payload);

        $response->assertStatus(404);
        $response->assertJSONFragment([
            'status'  => 'error',
            'message' => 'Patient not found'
        ]);
    }

    /**
     * Test: Create patient visit with invalid InternalPID (non-numeric)
     */
    public function testCreatePatientVisitInvalidInternalPID()
    {
        $payload = [
            'InternalPID' => 'invalid',
            'EpisodeID' => 'EPI001'
        ];

        $response = $this->withBodyFormat('json')->call('post', $this->endpoint, $payload);

        $response->assertStatus(400);
        $response->assertJSONFragment([
            'status'  => 'error',
            'message' => 'InternalPID is required and must be numeric'
        ]);
    }

}
