/**
 * Descomplicar® Crescimento Digital
 * https://descomplicar.pt
 */

<?php
/**
 * Integration tests for Encounter Creation Workflow (User Story 2).
 * 
 * These tests validate complete user stories and MUST FAIL initially (TDD RED phase).
 *
 * @package Care_API\Tests\Integration
 */

/**
 * Encounter workflow integration tests.
 * 
 * User Story: Doctor creates encounter with prescriptions
 */
class Test_Encounter_Workflow extends Care_API_Test_Case {

	/**
	 * Test complete encounter creation with prescriptions workflow.
	 * 
	 * @test
	 */
	public function test_doctor_creates_encounter_with_prescriptions_workflow() {
		// This test will fail initially as the workflow isn't implemented
		$this->markTestIncomplete( 'Encounter workflow not implemented yet - TDD RED phase' );
		
		// ARRANGE: Set up complete scenario
		wp_set_current_user( $this->doctor_user );
		$clinic_id = $this->create_test_clinic();
		
		// Create patient
		global $wpdb;
		$wpdb->insert( $wpdb->prefix . 'kc_patient_clinic_mappings', array(
			'patient_id' => $this->patient_user,
			'clinic_id'  => $clinic_id,
		));
		
		// Create appointment
		$appointment_id = $this->create_test_appointment( $clinic_id, $this->doctor_user, $this->patient_user );

		// STEP 1: Doctor creates medical encounter
		$encounter_data = array(
			'encounter_date'  => gmdate( 'Y-m-d' ),
			'appointment_id'  => $appointment_id,
			'patient_id'      => $this->patient_user,
			'clinic_id'       => $clinic_id,
			'description'     => 'Patient presents with mild fever (38.2°C), headache, and fatigue. Symptoms started 2 days ago. No cough or breathing difficulties. Physical examination reveals slightly elevated temperature, normal lung sounds, mild throat redness.',
			'chief_complaint' => 'Fever and headache for 2 days',
			'diagnosis'       => 'Viral syndrome (R50.9)',
			'treatment_plan'  => 'Rest, hydration, symptomatic treatment with paracetamol',
			'vital_signs'     => json_encode(array(
				'temperature' => '38.2°C',
				'blood_pressure' => '120/80',
				'heart_rate' => '85 bpm',
				'weight' => '70 kg',
			)),
			'status'          => 1,
		);

		$response = $this->make_request( '/wp-json/kivicare/v1/encounters', 'POST', $encounter_data, $this->doctor_user );
		
		// ASSERT: Encounter created successfully
		$this->assertRestResponse( $response, 201 );
		$encounter = $response->get_data();
		$this->assertEquals( $encounter_data['appointment_id'], $encounter['appointment_id'] );
		$this->assertEquals( $encounter_data['patient_id'], $encounter['patient_id'] );
		$encounter_id = $encounter['id'];

		// STEP 2: Verify encounter exists in database
		$db_encounter = $wpdb->get_row( 
			$wpdb->prepare( 
				"SELECT * FROM {$wpdb->prefix}kc_patient_encounters WHERE id = %d",
				$encounter_id
			)
		);
		$this->assertNotNull( $db_encounter );
		$this->assertEquals( $encounter_data['description'], $db_encounter->description );

		// STEP 3: Doctor adds prescriptions to encounter
		$prescriptions = array(
			array(
				'name'        => 'Paracetamol 500mg',
				'frequency'   => 'Every 8 hours as needed',
				'duration'    => '5 days',
				'instruction' => 'Take with water after meals. Do not exceed 4g per day.',
				'dosage'      => '1-2 tablets',
				'quantity'    => '15 tablets',
			),
			array(
				'name'        => 'Vitamin C 1000mg',
				'frequency'   => 'Once daily',
				'duration'    => '7 days',
				'instruction' => 'Take with breakfast to boost immune system.',
				'dosage'      => '1 tablet',
				'quantity'    => '7 tablets',
			),
		);

		$prescription_ids = array();
		foreach ( $prescriptions as $prescription_data ) {
			$response = $this->make_request( 
				"/wp-json/kivicare/v1/encounters/{$encounter_id}/prescriptions", 
				'POST', 
				$prescription_data, 
				$this->doctor_user 
			);
			
			$this->assertRestResponse( $response, 201 );
			$prescription = $response->get_data();
			$this->assertEquals( $encounter_id, $prescription['encounter_id'] );
			$this->assertEquals( $this->patient_user, $prescription['patient_id'] );
			$prescription_ids[] = $prescription['id'];
		}

		// STEP 4: Verify prescriptions are linked to encounter
		$encounter_prescriptions_response = $this->make_request( 
			"/wp-json/kivicare/v1/encounters/{$encounter_id}/prescriptions", 
			'GET', 
			array(), 
			$this->doctor_user 
		);
		
		$this->assertRestResponse( $encounter_prescriptions_response, 200 );
		$encounter_prescriptions = $encounter_prescriptions_response->get_data();
		$this->assertCount( 2, $encounter_prescriptions );
		
		// Verify prescription details
		foreach ( $encounter_prescriptions as $i => $prescription ) {
			$this->assertEquals( $prescriptions[$i]['name'], $prescription['name'] );
			$this->assertEquals( $prescriptions[$i]['frequency'], $prescription['frequency'] );
		}

		// STEP 5: Verify appointment status was updated to completed
		$appointment_response = $this->make_request( "/wp-json/kivicare/v1/appointments/{$appointment_id}", 'GET', array(), $this->doctor_user );
		$this->assertRestResponse( $appointment_response, 200 );
		
		$appointment = $appointment_response->get_data();
		$this->assertEquals( 'completed', $appointment['status'] );

		// STEP 6: Verify automatic bill generation
		$bill = $wpdb->get_row( 
			$wpdb->prepare( 
				"SELECT * FROM {$wpdb->prefix}kc_bills WHERE encounter_id = %d",
				$encounter_id
			)
		);
		$this->assertNotNull( $bill, 'Bill should be automatically generated for encounter' );
		$this->assertEquals( $encounter_id, $bill->encounter_id );
		$this->assertEquals( $appointment_id, $bill->appointment_id );
		$this->assertEquals( 'unpaid', $bill->payment_status );

		// STEP 7: Verify patient can view encounter and prescriptions
		$patient_encounter_response = $this->make_request( "/wp-json/kivicare/v1/encounters/{$encounter_id}", 'GET', array(), $this->patient_user );
		$this->assertRestResponse( $patient_encounter_response, 200 );
		
		$patient_encounter = $patient_encounter_response->get_data();
		$this->assertEquals( $encounter_id, $patient_encounter['id'] );
		// Sensitive medical details should be filtered for patient view
		$this->assertArrayNotHasKey( 'vital_signs', $patient_encounter );
	}

	/**
	 * Test encounter creation triggers proper workflow events.
	 * 
	 * @test
	 */
	public function test_encounter_creation_workflow_events() {
		// This test will fail initially as workflow events aren't implemented
		$this->markTestIncomplete( 'Encounter workflow events not implemented yet - TDD RED phase' );
		
		// ARRANGE: Setup scenario
		$clinic_id = $this->create_test_clinic();
		$appointment_id = $this->create_test_appointment( $clinic_id, $this->doctor_user, $this->patient_user );
		
		// Track WordPress actions/hooks that should fire
		$actions_fired = array();
		$test_instance = $this;
		
		add_action( 'kivicare_encounter_created', function( $encounter_id, $encounter_data ) use ( &$actions_fired, $test_instance ) {
			$actions_fired['encounter_created'] = array( $encounter_id, $encounter_data );
		}, 10, 2 );
		
		add_action( 'kivicare_appointment_completed', function( $appointment_id ) use ( &$actions_fired, $test_instance ) {
			$actions_fired['appointment_completed'] = $appointment_id;
		} );
		
		add_action( 'kivicare_bill_generated', function( $bill_id, $encounter_id ) use ( &$actions_fired, $test_instance ) {
			$actions_fired['bill_generated'] = array( $bill_id, $encounter_id );
		}, 10, 2 );

		// ACT: Create encounter
		$encounter_data = array(
			'appointment_id' => $appointment_id,
			'description'    => 'Test encounter for workflow events',
			'status'         => 1,
		);

		$response = $this->make_request( '/wp-json/kivicare/v1/encounters', 'POST', $encounter_data, $this->doctor_user );
		$this->assertRestResponse( $response, 201 );

		// ASSERT: All workflow events were triggered
		$this->assertArrayHasKey( 'encounter_created', $actions_fired );
		$this->assertArrayHasKey( 'appointment_completed', $actions_fired );
		$this->assertArrayHasKey( 'bill_generated', $actions_fired );
	}

	/**
	 * Test encounter data integrity and validation.
	 * 
	 * @test
	 */
	public function test_encounter_data_integrity() {
		// This test will fail initially as validation isn't implemented
		$this->markTestIncomplete( 'Encounter data validation not implemented yet - TDD RED phase' );
		
		$clinic_id = $this->create_test_clinic();
		$appointment_id = $this->create_test_appointment( $clinic_id, $this->doctor_user, $this->patient_user );

		// Test various validation scenarios
		$validation_tests = array(
			// Missing required fields
			array(
				'data'   => array( 'description' => 'Test encounter' ),
				'status' => 400,
				'code'   => 'rest_missing_callback_param',
			),
			// Invalid appointment ID
			array(
				'data'   => array( 'appointment_id' => 99999, 'description' => 'Test' ),
				'status' => 400,
				'code'   => 'invalid_appointment',
			),
			// Encounter for completed appointment
			array(
				'setup'  => function() use ( $appointment_id ) {
					global $wpdb;
					$wpdb->update( 
						$wpdb->prefix . 'kc_appointments', 
						array( 'status' => 0 ), // Mark as completed
						array( 'id' => $appointment_id )
					);
				},
				'data'   => array( 'appointment_id' => $appointment_id, 'description' => 'Test' ),
				'status' => 409,
				'code'   => 'appointment_already_completed',
			),
		);

		foreach ( $validation_tests as $test ) {
			if ( isset( $test['setup'] ) ) {
				$test['setup']();
			}

			$response = $this->make_request( '/wp-json/kivicare/v1/encounters', 'POST', $test['data'], $this->doctor_user );
			$this->assertRestResponse( $response, $test['status'] );
			
			if ( isset( $test['code'] ) ) {
				$error_data = $response->get_data();
				$this->assertEquals( $test['code'], $error_data['code'] );
			}
		}
	}

	/**
	 * Test prescription validation and drug interaction checks.
	 * 
	 * @test
	 */
	public function test_prescription_validation_workflow() {
		// This test will fail initially as prescription validation isn't implemented
		$this->markTestIncomplete( 'Prescription validation not implemented yet - TDD RED phase' );
		
		// ARRANGE: Create encounter
		$clinic_id = $this->create_test_clinic();
		$appointment_id = $this->create_test_appointment( $clinic_id, $this->doctor_user, $this->patient_user );
		
		$encounter_response = $this->make_request( '/wp-json/kivicare/v1/encounters', 'POST', array(
			'appointment_id' => $appointment_id,
			'description'    => 'Test encounter for prescription validation',
		), $this->doctor_user );
		
		$encounter_id = $encounter_response->get_data()['id'];

		// Test prescription validation
		$prescription_tests = array(
			// Valid prescription
			array(
				'data'   => array( 'name' => 'Paracetamol 500mg', 'frequency' => 'Every 8h', 'duration' => '7 days' ),
				'status' => 201,
			),
			// Missing medication name
			array(
				'data'   => array( 'frequency' => 'Every 8h', 'duration' => '7 days' ),
				'status' => 400,
			),
			// Invalid duration format
			array(
				'data'   => array( 'name' => 'Test Med', 'frequency' => 'Daily', 'duration' => 'forever' ),
				'status' => 400,
			),
		);

		foreach ( $prescription_tests as $test ) {
			$response = $this->make_request( 
				"/wp-json/kivicare/v1/encounters/{$encounter_id}/prescriptions", 
				'POST', 
				$test['data'], 
				$this->doctor_user 
			);
			
			$this->assertRestResponse( $response, $test['status'] );
		}
	}

	/**
	 * Test encounter permissions and access control.
	 * 
	 * @test
	 */
	public function test_encounter_permissions_workflow() {
		// This test will fail initially as permissions aren't implemented
		$this->markTestIncomplete( 'Encounter permissions not implemented yet - TDD RED phase' );
		
		// ARRANGE: Create encounter
		$clinic_id = $this->create_test_clinic();
		$appointment_id = $this->create_test_appointment( $clinic_id, $this->doctor_user, $this->patient_user );
		
		$encounter_data = array(
			'appointment_id' => $appointment_id,
			'description'    => 'Test encounter for permissions',
		);

		// Test role-based permissions
		$permission_tests = array(
			array( 'user_id' => $this->doctor_user, 'expected_status' => 201 ),       // Doctor can create
			array( 'user_id' => $this->admin_user, 'expected_status' => 201 ),       // Admin can create
			array( 'user_id' => $this->patient_user, 'expected_status' => 403 ),     // Patient cannot create
			array( 'user_id' => $this->receptionist_user, 'expected_status' => 403 ), // Receptionist cannot create
		);

		foreach ( $permission_tests as $i => $test ) {
			// Create unique appointment for each test
			$test_appointment_id = $this->create_test_appointment( $clinic_id, $this->doctor_user, $this->patient_user );
			$test_data = $encounter_data;
			$test_data['appointment_id'] = $test_appointment_id;
			
			$response = $this->make_request( '/wp-json/kivicare/v1/encounters', 'POST', $test_data, $test['user_id'] );
			$this->assertRestResponse( $response, $test['expected_status'] );
		}
	}
}