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

<?php
/**
 * Test database setup for KiviCare API tests.
 *
 * @package Care_API\Tests
 */

/**
 * Class to handle test database setup.
 */
class Care_API_Test_Database {

	/**
	 * Create necessary KiviCare tables for testing.
	 */
	public static function create_tables() {
		global $wpdb;

		$charset_collate = $wpdb->get_charset_collate();

		// Clinics table
		$table_name = $wpdb->prefix . 'kc_clinics';
		$sql = "CREATE TABLE $table_name (
			id bigint(20) NOT NULL AUTO_INCREMENT,
			name varchar(191) NOT NULL,
			email varchar(191) DEFAULT NULL,
			telephone_no varchar(191) DEFAULT NULL,
			specialties longtext,
			address text,
			city varchar(191) DEFAULT NULL,
			state varchar(191) DEFAULT NULL,
			country varchar(191) DEFAULT NULL,
			postal_code varchar(191) DEFAULT NULL,
			status tinyint(1) DEFAULT 1,
			clinic_admin_id bigint(20) DEFAULT NULL,
			clinic_logo bigint(20) DEFAULT NULL,
			profile_image bigint(20) DEFAULT NULL,
			extra longtext,
			created_at datetime DEFAULT CURRENT_TIMESTAMP,
			PRIMARY KEY (id),
			KEY clinic_admin_id (clinic_admin_id),
			KEY status (status)
		) $charset_collate;";
		
		require_once ABSPATH . 'wp-admin/includes/upgrade.php';
		dbDelta( $sql );

		// Appointments table
		$table_name = $wpdb->prefix . 'kc_appointments';
		$sql = "CREATE TABLE $table_name (
			id bigint(20) NOT NULL AUTO_INCREMENT,
			appointment_start_date date NOT NULL,
			appointment_start_time time NOT NULL,
			appointment_end_date date DEFAULT NULL,
			appointment_end_time time DEFAULT NULL,
			visit_type varchar(191) DEFAULT NULL,
			clinic_id bigint(20) NOT NULL,
			doctor_id bigint(20) NOT NULL,
			patient_id bigint(20) NOT NULL,
			description text,
			status tinyint(1) DEFAULT 1,
			appointment_report longtext,
			created_at datetime DEFAULT CURRENT_TIMESTAMP,
			PRIMARY KEY (id),
			KEY clinic_id (clinic_id),
			KEY doctor_id (doctor_id),
			KEY patient_id (patient_id),
			KEY appointment_date (appointment_start_date, appointment_start_time),
			KEY status (status)
		) $charset_collate;";
		
		dbDelta( $sql );

		// Patient encounters table
		$table_name = $wpdb->prefix . 'kc_patient_encounters';
		$sql = "CREATE TABLE $table_name (
			id bigint(20) NOT NULL AUTO_INCREMENT,
			encounter_date date NOT NULL,
			clinic_id bigint(20) NOT NULL,
			doctor_id bigint(20) NOT NULL,
			patient_id bigint(20) NOT NULL,
			appointment_id bigint(20) DEFAULT NULL,
			description text,
			status tinyint(1) DEFAULT 1,
			added_by bigint(20) DEFAULT NULL,
			template_id bigint(20) DEFAULT NULL,
			created_at datetime DEFAULT CURRENT_TIMESTAMP,
			PRIMARY KEY (id),
			KEY clinic_id (clinic_id),
			KEY doctor_id (doctor_id),
			KEY patient_id (patient_id),
			KEY appointment_id (appointment_id),
			KEY encounter_date (encounter_date)
		) $charset_collate;";
		
		dbDelta( $sql );

		// Prescriptions table
		$table_name = $wpdb->prefix . 'kc_prescription';
		$sql = "CREATE TABLE $table_name (
			id bigint(20) NOT NULL AUTO_INCREMENT,
			encounter_id bigint(20) NOT NULL,
			patient_id bigint(20) NOT NULL,
			name text NOT NULL,
			frequency varchar(199) DEFAULT NULL,
			duration varchar(199) DEFAULT NULL,
			instruction text,
			added_by bigint(20) DEFAULT NULL,
			is_from_template tinyint(1) DEFAULT 0,
			created_at datetime DEFAULT CURRENT_TIMESTAMP,
			PRIMARY KEY (id),
			KEY encounter_id (encounter_id),
			KEY patient_id (patient_id)
		) $charset_collate;";
		
		dbDelta( $sql );

		// Bills table
		$table_name = $wpdb->prefix . 'kc_bills';
		$sql = "CREATE TABLE $table_name (
			id bigint(20) NOT NULL AUTO_INCREMENT,
			encounter_id bigint(20) DEFAULT NULL,
			appointment_id bigint(20) DEFAULT NULL,
			clinic_id bigint(20) NOT NULL,
			title varchar(191) DEFAULT NULL,
			total_amount varchar(50) DEFAULT NULL,
			discount varchar(50) DEFAULT '0',
			actual_amount varchar(50) DEFAULT NULL,
			status bigint(20) DEFAULT 1,
			payment_status varchar(10) DEFAULT 'unpaid',
			created_at datetime DEFAULT CURRENT_TIMESTAMP,
			PRIMARY KEY (id),
			KEY encounter_id (encounter_id),
			KEY appointment_id (appointment_id),
			KEY clinic_id (clinic_id),
			KEY payment_status (payment_status)
		) $charset_collate;";
		
		dbDelta( $sql );

		// Services table
		$table_name = $wpdb->prefix . 'kc_services';
		$sql = "CREATE TABLE $table_name (
			id bigint(20) NOT NULL AUTO_INCREMENT,
			type varchar(191) DEFAULT 'system_service',
			name varchar(191) NOT NULL,
			price varchar(50) DEFAULT '0',
			status tinyint(1) DEFAULT 1,
			created_at datetime DEFAULT CURRENT_TIMESTAMP,
			PRIMARY KEY (id),
			KEY type (type),
			KEY status (status)
		) $charset_collate;";
		
		dbDelta( $sql );

		// Doctor clinic mappings
		$table_name = $wpdb->prefix . 'kc_doctor_clinic_mappings';
		$sql = "CREATE TABLE $table_name (
			id bigint(20) NOT NULL AUTO_INCREMENT,
			doctor_id bigint(20) NOT NULL,
			clinic_id bigint(20) NOT NULL,
			created_at datetime DEFAULT CURRENT_TIMESTAMP,
			PRIMARY KEY (id),
			UNIQUE KEY doctor_clinic (doctor_id, clinic_id),
			KEY doctor_id (doctor_id),
			KEY clinic_id (clinic_id)
		) $charset_collate;";
		
		dbDelta( $sql );

		// Patient clinic mappings
		$table_name = $wpdb->prefix . 'kc_patient_clinic_mappings';
		$sql = "CREATE TABLE $table_name (
			id bigint(20) NOT NULL AUTO_INCREMENT,
			patient_id bigint(20) NOT NULL,
			clinic_id bigint(20) NOT NULL,
			created_at datetime DEFAULT CURRENT_TIMESTAMP,
			PRIMARY KEY (id),
			UNIQUE KEY patient_clinic (patient_id, clinic_id),
			KEY patient_id (patient_id),
			KEY clinic_id (clinic_id)
		) $charset_collate;";
		
		dbDelta( $sql );

		// Appointment service mappings
		$table_name = $wpdb->prefix . 'kc_appointment_service_mapping';
		$sql = "CREATE TABLE $table_name (
			id bigint(20) NOT NULL AUTO_INCREMENT,
			appointment_id bigint(20) NOT NULL,
			service_id bigint(20) NOT NULL,
			created_at datetime DEFAULT CURRENT_TIMESTAMP,
			PRIMARY KEY (id),
			KEY appointment_id (appointment_id),
			KEY service_id (service_id)
		) $charset_collate;";
		
		dbDelta( $sql );
	}

	/**
	 * Insert sample data for testing.
	 */
	public static function insert_sample_data() {
		global $wpdb;

		// Create sample clinic
		$wpdb->insert(
			$wpdb->prefix . 'kc_clinics',
			array(
				'name'         => 'Test Clinic',
				'email'        => 'test@clinic.com',
				'telephone_no' => '+351912345678',
				'address'      => 'Rua de Teste, 123',
				'city'         => 'Lisboa',
				'state'        => 'Lisboa',
				'country'      => 'Portugal',
				'postal_code'  => '1000-001',
				'status'       => 1,
				'created_at'   => current_time( 'mysql' ),
			)
		);

		$clinic_id = $wpdb->insert_id;

		// Create sample services
		$services = array(
			array( 'name' => 'General Consultation', 'price' => '50.00' ),
			array( 'name' => 'Blood Test', 'price' => '25.00' ),
			array( 'name' => 'X-Ray', 'price' => '75.00' ),
		);

		foreach ( $services as $service ) {
			$wpdb->insert(
				$wpdb->prefix . 'kc_services',
				array(
					'name'       => $service['name'],
					'price'      => $service['price'],
					'status'     => 1,
					'created_at' => current_time( 'mysql' ),
				)
			);
		}

		// Store clinic ID for use in tests
		update_option( 'kivicare_api_test_clinic_id', $clinic_id );
	}

	/**
	 * Clean up test data.
	 */
	public static function cleanup() {
		global $wpdb;

		$tables = array(
			'kc_clinics',
			'kc_appointments',
			'kc_patient_encounters',
			'kc_prescription',
			'kc_bills',
			'kc_services',
			'kc_doctor_clinic_mappings',
			'kc_patient_clinic_mappings',
			'kc_appointment_service_mapping',
		);

		foreach ( $tables as $table ) {
			// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
			$wpdb->query( "TRUNCATE TABLE {$wpdb->prefix}$table" );
		}

		delete_option( 'kivicare_api_test_clinic_id' );
	}
}