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

<?php
/**
 * Mock KiviCare plugin functionality for testing.
 *
 * @package Care_API\Tests\Mocks
 */

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Mock KiviCare plugin activation check.
 */
function mock_kivicare_activation() {
	// Mock the KiviCare plugin as active
	add_filter( 'active_plugins', function( $plugins ) {
		$plugins[] = 'kivicare-clinic-&-patient-management-system/kivicare-clinic-&-patient-management-system.php';
		return $plugins;
	});
}

/**
 * Mock KiviCare user roles.
 */
function mock_kivicare_roles() {
	// Add KiviCare roles that don't exist in core WordPress
	add_role( 'doctor', 'Doctor', array(
		'read'                    => true,
		'manage_kivicare_api'     => true,
		'kivicare_api_medical_access' => true,
	));

	add_role( 'patient', 'Patient', array(
		'read'                        => true,
		'manage_kivicare_api'         => true,
		'kivicare_api_patient_access' => true,
	));

	add_role( 'kivicare_receptionist', 'Receptionist', array(
		'read'                           => true,
		'manage_kivicare_api'            => true,
		'kivicare_api_reception_access'  => true,
	));

	// Add capabilities to administrator role
	$admin_role = get_role( 'administrator' );
	if ( $admin_role ) {
		$admin_role->add_cap( 'manage_kivicare_api' );
		$admin_role->add_cap( 'kivicare_api_full_access' );
	}
}

/**
 * Mock KiviCare constants.
 */
function mock_kivicare_constants() {
	if ( ! defined( 'KIVI_CARE_VERSION' ) ) {
		define( 'KIVI_CARE_VERSION', '2.5.0' );
	}
	
	if ( ! defined( 'KIVI_CARE_DIR' ) ) {
		define( 'KIVI_CARE_DIR', WP_PLUGIN_DIR . '/kivicare-clinic-&-patient-management-system/' );
	}
}

/**
 * Mock KiviCare helper functions.
 */
if ( ! function_exists( 'kcGetDefaultClinicId' ) ) {
	/**
	 * Mock function to get default clinic ID.
	 *
	 * @return int
	 */
	function kcGetDefaultClinicId() {
		return get_option( 'kivicare_api_test_clinic_id', 1 );
	}
}

if ( ! function_exists( 'kcCheckPermission' ) ) {
	/**
	 * Mock function to check KiviCare permissions.
	 *
	 * @param string $permission Permission to check.
	 * @return bool
	 */
	function kcCheckPermission( $permission ) {
		return current_user_can( 'manage_kivicare_api' );
	}
}

/**
 * Initialize mock KiviCare functionality.
 */
function init_mock_kivicare() {
	mock_kivicare_activation();
	mock_kivicare_constants();
	
	// Wait until init to add roles
	add_action( 'init', 'mock_kivicare_roles' );
}

// Initialize mocks
init_mock_kivicare();