<?php
/**
 * Descomplicar® Crescimento Digital
 * https://descomplicar.pt
 */

/**
 * Care API Initialization
 *
 * Central initialization class that loads and coordinates all API components
 *
 * @package Care_API
 * @since   1.0.0
 */

namespace Care_API;

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

// Include Security Manager for hardened authentication
require_once plugin_dir_path( __FILE__ ) . 'class-security-manager.php';

/**
 * Main API initialization class
 *
 * Coordinates the loading and initialization of all API components
 * following the Master Orchestrator Supreme architecture pattern
 *
 * @since 1.0.0
 */
class API_Init {

	/**
	 * The single instance of the class.
	 *
	 * @var API_Init
	 * @since 1.0.0
	 */
	protected static $_instance = null;

	/**
	 * Plugin version
	 *
	 * @var string
	 */
	const VERSION = '1.0.0';

	/**
	 * REST API namespace.
	 *
	 * @var string
	 */
	const API_NAMESPACE = 'care/v1';

	/**
	 * Minimum PHP version required
	 *
	 * @var string
	 */
	const MIN_PHP_VERSION = '7.4';

	/**
	 * Minimum WordPress version required
	 *
	 * @var string
	 */
	const MIN_WP_VERSION = '5.0';

	/**
	 * Main API_Init Instance.
	 *
	 * Ensures only one instance of API_Init is loaded or can be loaded.
	 *
	 * @since 1.0.0
	 * @static
	 * @return API_Init - Main instance.
	 */
	public static function instance() {
		if ( is_null( self::$_instance ) ) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}

	/**
	 * API_Init Constructor.
	 */
	public function __construct() {
		$this->init();
	}

	/**
	 * Initialize the API
	 *
	 * @since 1.0.0
	 */
	private function init() {
		// Check system requirements
		if ( ! $this->check_requirements() ) {
			return;
		}

		// Initialize error handler first
		$this->init_error_handler();

		// Load all dependencies
		$this->load_dependencies();
		
		// Initialize services
		$this->init_services();
		
		// Initialize hooks
		$this->init_hooks();

		// Log successful initialization
		error_log( 'Care API initialized successfully - Version ' . self::VERSION );
	}

	/**
	 * Check system requirements
	 *
	 * @return bool Requirements met
	 * @since 1.0.0
	 */
	private function check_requirements() {
		// Check PHP version
		if ( version_compare( PHP_VERSION, self::MIN_PHP_VERSION, '<' ) ) {
			add_action( 'admin_notices', function() {
				echo '<div class="notice notice-error"><p>';
				echo sprintf( 
					'Care API requires PHP version %s or higher. Current version: %s', 
					self::MIN_PHP_VERSION, 
					PHP_VERSION 
				);
				echo '</p></div>';
			});
			return false;
		}

		// Check WordPress version
		if ( version_compare( get_bloginfo( 'version' ), self::MIN_WP_VERSION, '<' ) ) {
			add_action( 'admin_notices', function() {
				echo '<div class="notice notice-error"><p>';
				echo sprintf( 
					'Care API requires WordPress version %s or higher. Current version: %s', 
					self::MIN_WP_VERSION, 
					get_bloginfo( 'version' ) 
				);
				echo '</p></div>';
			});
			return false;
		}

		// Check if Care is active
		if ( ! $this->is_kivicare_active() ) {
			add_action( 'admin_notices', array( $this, 'kivicare_dependency_notice' ) );
			return false;
		}

		// Check required database tables
		if ( ! $this->check_kivicare_tables() ) {
			add_action( 'admin_notices', array( $this, 'database_tables_notice' ) );
			return false;
		}

		return true;
	}

	/**
	 * Initialize error handler
	 *
	 * @since 1.0.0
	 */
	private function init_error_handler() {
		if ( ! class_exists( 'Care_API\\Utils\\Error_Handler' ) ) {
			require_once CARE_API_ABSPATH . 'includes/utils/class-error-handler.php';
			Utils\Error_Handler::init();
		}
	}

	/**
	 * Load all required dependencies
	 *
	 * @since 1.0.0
	 */
	private function load_dependencies() {
		// Load security utilities first
		require_once CARE_API_ABSPATH . 'includes/utils/class-database-security-layer.php';
		require_once CARE_API_ABSPATH . 'includes/utils/class-secure-query-builder.php';

		// Load other utilities
		require_once CARE_API_ABSPATH . 'includes/utils/class-input-validator.php';
		require_once CARE_API_ABSPATH . 'includes/utils/class-api-logger.php';

		// Load models
		require_once CARE_API_ABSPATH . 'includes/models/class-clinic.php';
		require_once CARE_API_ABSPATH . 'includes/models/class-patient.php';
		require_once CARE_API_ABSPATH . 'includes/models/class-doctor.php';
		require_once CARE_API_ABSPATH . 'includes/models/class-appointment.php';
		require_once CARE_API_ABSPATH . 'includes/models/class-encounter.php';
		require_once CARE_API_ABSPATH . 'includes/models/class-prescription.php';
		require_once CARE_API_ABSPATH . 'includes/models/class-bill.php';
		require_once CARE_API_ABSPATH . 'includes/models/class-service.php';

		// Load authentication and permission services
		require_once CARE_API_ABSPATH . 'includes/services/class-jwt-service.php';
		require_once CARE_API_ABSPATH . 'includes/services/class-auth-service.php';
		require_once CARE_API_ABSPATH . 'includes/services/class-permission-service.php';
		require_once CARE_API_ABSPATH . 'includes/services/class-session-service.php';

		// Load core services
		require_once CARE_API_ABSPATH . 'includes/services/class-integration-service.php';
		require_once CARE_API_ABSPATH . 'includes/services/class-response-standardization-service.php';
		require_once CARE_API_ABSPATH . 'includes/services/class-cache-service.php';
		require_once CARE_API_ABSPATH . 'includes/services/class-performance-monitoring-service.php';
		require_once CARE_API_ABSPATH . 'includes/services/class-clinic-isolation-service.php';

		// Load middleware
		require_once CARE_API_ABSPATH . 'includes/middleware/class-jwt-middleware.php';

		// Load database services
		require_once CARE_API_ABSPATH . 'includes/services/database/class-clinic-service.php';
		require_once CARE_API_ABSPATH . 'includes/services/database/class-patient-service.php';
		require_once CARE_API_ABSPATH . 'includes/services/database/class-doctor-service.php';
		require_once CARE_API_ABSPATH . 'includes/services/database/class-appointment-service.php';
		require_once CARE_API_ABSPATH . 'includes/services/database/class-encounter-service.php';
		require_once CARE_API_ABSPATH . 'includes/services/database/class-prescription-service.php';
		require_once CARE_API_ABSPATH . 'includes/services/database/class-bill-service.php';

		// Load REST API endpoints
		require_once CARE_API_ABSPATH . 'includes/endpoints/class-auth-endpoints.php';
		require_once CARE_API_ABSPATH . 'includes/endpoints/class-clinic-endpoints.php';
		require_once CARE_API_ABSPATH . 'includes/endpoints/class-patient-endpoints.php';
		require_once CARE_API_ABSPATH . 'includes/endpoints/class-appointment-endpoints.php';
		require_once CARE_API_ABSPATH . 'includes/endpoints/class-doctor-endpoints.php';
		require_once CARE_API_ABSPATH . 'includes/endpoints/class-encounter-endpoints.php';
		require_once CARE_API_ABSPATH . 'includes/endpoints/class-prescription-endpoints.php';
		require_once CARE_API_ABSPATH . 'includes/endpoints/class-bill-endpoints.php';

		// Load admin documentation
		if ( is_admin() ) {
			require_once CARE_API_ABSPATH . 'admin/class-docs-admin.php';
		}

		// Load testing framework
		if ( defined( 'CARE_API_DEBUG' ) && CARE_API_DEBUG ) {
			require_once CARE_API_ABSPATH . 'includes/testing/class-unit-test-suite.php';
		}
	}

	/**
	 * Initialize all services
	 *
	 * @since 1.0.0
	 */
	private function init_services() {
		// Initialize security layer first
		if ( class_exists( 'Care_API\\Utils\\Database_Security_Layer' ) ) {
			Utils\Database_Security_Layer::init();
		}

		// Initialize utilities
		if ( class_exists( 'Care_API\\Utils\\API_Logger' ) ) {
			Utils\API_Logger::init();
		}

		// Initialize authentication services
		if ( class_exists( 'Care_API\\Services\\Auth_Service' ) ) {
			Services\Auth_Service::init();
		}
		if ( class_exists( 'Care_API\\Services\\Permission_Service' ) ) {
			Services\Permission_Service::init();
		}
		if ( class_exists( 'Care_API\\Services\\Session_Service' ) ) {
			Services\Session_Service::init();
		}

		// Initialize core services
		if ( class_exists( 'Care_API\\Services\\Integration_Service' ) ) {
			Services\Integration_Service::init();
		}
		if ( class_exists( 'Care_API\\Services\\Response_Standardization_Service' ) ) {
			Services\Response_Standardization_Service::init();
		}
		if ( class_exists( 'Care_API\\Services\\Cache_Service' ) ) {
			Services\Cache_Service::init();
		}
		if ( class_exists( 'Care_API\\Services\\Performance_Monitoring_Service' ) ) {
			Services\Performance_Monitoring_Service::init();
		}
		if ( class_exists( 'Care_API\\Services\\Clinic_Isolation_Service' ) ) {
			Services\Clinic_Isolation_Service::init();
		}

		// Initialize middleware
		if ( class_exists( 'Care_API\\Middleware\\JWT_Middleware' ) ) {
			Middleware\JWT_Middleware::init();
		}

		// Initialize database services
		if ( class_exists( 'Care_API\\Services\\Database\\Clinic_Service' ) ) {
			Services\Database\Clinic_Service::init();
		}
		if ( class_exists( 'Care_API\\Services\\Database\\Patient_Service' ) ) {
			Services\Database\Patient_Service::init();
		}
		if ( class_exists( 'Care_API\\Services\\Database\\Doctor_Service' ) ) {
			Services\Database\Doctor_Service::init();
		}
		if ( class_exists( 'Care_API\\Services\\Database\\Appointment_Service' ) ) {
			Services\Database\Appointment_Service::init();
		}
		if ( class_exists( 'Care_API\\Services\\Database\\Encounter_Service' ) ) {
			Services\Database\Encounter_Service::init();
		}
		if ( class_exists( 'Care_API\\Services\\Database\\Prescription_Service' ) ) {
			Services\Database\Prescription_Service::init();
		}
		if ( class_exists( 'Care_API\\Services\\Database\\Bill_Service' ) ) {
			Services\Database\Bill_Service::init();
		}

		// Initialize testing framework in debug mode
		if ( defined( 'CARE_API_DEBUG' ) && CARE_API_DEBUG && class_exists( 'Care_API\\Testing\\Unit_Test_Suite' ) ) {
			Testing\Unit_Test_Suite::init();
		}
	}

	/**
	 * Initialize WordPress hooks
	 *
	 * @since 1.0.0
	 */
	private function init_hooks() {
		// REST API initialization
		add_action( 'rest_api_init', array( $this, 'register_rest_routes' ) );
		
		// WordPress initialization hooks
		add_action( 'init', array( $this, 'init_wordpress_integration' ) );
		add_action( 'wp_loaded', array( $this, 'init_late_loading' ) );

		// Admin hooks
		if ( is_admin() ) {
			add_action( 'admin_init', array( $this, 'init_admin' ) );
			add_action( 'admin_menu', array( $this, 'add_admin_menu' ) );
		}

		// AJAX hooks for frontend integration
		add_action( 'wp_ajax_care_api_status', array( $this, 'ajax_api_status' ) );
		add_action( 'wp_ajax_nopriv_care_api_status', array( $this, 'ajax_api_status' ) );

		// Cron hooks for maintenance tasks
		add_action( 'kivicare_daily_maintenance', array( $this, 'daily_maintenance' ) );
		if ( ! wp_next_scheduled( 'kivicare_daily_maintenance' ) ) {
			wp_schedule_event( time(), 'daily', 'kivicare_daily_maintenance' );
		}

		// Response headers filter
		add_filter( 'rest_pre_serve_request', array( $this, 'rest_pre_serve_request' ), 10, 4 );
	}

	/**
	 * Check if Care plugin is active.
	 *
	 * @return bool
	 */
	private function is_kivicare_active() {
		// Check if Care functions exist (more reliable than checking if plugin is active)
		return function_exists( 'kc_get_current_user_role' ) || 
			   class_exists( 'KiviCare' ) ||
			   is_plugin_active( 'kivicare-clinic-&-patient-management-system/kivicare-clinic-&-patient-management-system.php' );
	}

	/**
	 * Check if required Care database tables exist.
	 *
	 * @return bool
	 */
	private function check_kivicare_tables() {
		global $wpdb;

		$required_tables = array(
			'kc_clinics',
			'kc_appointments',
			'kc_patient_encounters',
			'kc_prescription',
			'kc_bills',
			'kc_services',
			'kc_doctor_clinic_mappings',
		);

		foreach ( $required_tables as $table ) {
			$table_name = $wpdb->prefix . $table;
			$table_exists = $wpdb->get_var( $wpdb->prepare( "SHOW TABLES LIKE %s", $table_name ) );
			
			if ( $table_name !== $table_exists ) {
				return false;
			}
		}

		return true;
	}

	/**
	 * Display admin notice for Care dependency.
	 */
	public function kivicare_dependency_notice() {
		?>
		<div class="notice notice-error">
			<p>
				<strong><?php esc_html_e( 'Care API Error:', 'care-api' ); ?></strong>
				<?php esc_html_e( 'Care Plugin is required for Care API to work properly.', 'care-api' ); ?>
			</p>
		</div>
		<?php
	}

	/**
	 * Display admin notice for missing database tables.
	 */
	public function database_tables_notice() {
		?>
		<div class="notice notice-error">
			<p>
				<strong><?php esc_html_e( 'Care API Error:', 'care-api' ); ?></strong>
				<?php esc_html_e( 'Required Care database tables are missing. Please ensure Care plugin is properly activated.', 'care-api' ); ?>
			</p>
		</div>
		<?php
	}

	/**
	 * Register all REST API routes
	 *
	 * @since 1.0.0
	 */
	public function register_rest_routes() {
		try {
			// Register authentication endpoints
			if ( class_exists( 'Care_API\\Endpoints\\Auth_Endpoints' ) ) {
				Endpoints\Auth_Endpoints::register_routes();
			}

			// Register main entity endpoints
			if ( class_exists( 'Care_API\\Endpoints\\Clinic_Endpoints' ) ) {
				Endpoints\Clinic_Endpoints::register_routes();
			}
			if ( class_exists( 'Care_API\\Endpoints\\Patient_Endpoints' ) ) {
				Endpoints\Patient_Endpoints::register_routes();
			}
			if ( class_exists( 'Care_API\\Endpoints\\Doctor_Endpoints' ) ) {
				Endpoints\Doctor_Endpoints::register_routes();
			}
			if ( class_exists( 'Care_API\\Endpoints\\Appointment_Endpoints' ) ) {
				Endpoints\Appointment_Endpoints::register_routes();
			}
			if ( class_exists( 'Care_API\\Endpoints\\Encounter_Endpoints' ) ) {
				Endpoints\Encounter_Endpoints::register_routes();
			}
			if ( class_exists( 'Care_API\\Endpoints\\Prescription_Endpoints' ) ) {
				Endpoints\Prescription_Endpoints::register_routes();
			}
			if ( class_exists( 'Care_API\\Endpoints\\Bill_Endpoints' ) ) {
				Endpoints\Bill_Endpoints::register_routes();
			}

			// Register utility endpoints
			$this->register_utility_routes();

			// Allow plugins to hook into REST API registration
			do_action( 'care_api_register_rest_routes' );

		} catch ( Exception $e ) {
			if ( class_exists( 'Care_API\\Utils\\Error_Handler' ) ) {
				Utils\Error_Handler::handle_exception( $e );
			} else {
				error_log( 'Care API Route Registration Error: ' . $e->getMessage() );
			}
		}
	}


	/**
	 * Register utility routes
	 *
	 * @since 1.0.0
	 */
	private function register_utility_routes() {
		// API status endpoint - SECURED WITH AUTHENTICATION
		register_rest_route( self::API_NAMESPACE, '/status', array(
			'methods' => 'GET',
			'callback' => array( $this, 'get_api_status' ),
			'permission_callback' => array( $this, 'check_admin_permissions' )
		));

		// Health check endpoint - SECURED WITH RATE LIMITING
		register_rest_route( self::API_NAMESPACE, '/health', array(
			'methods' => 'GET',
			'callback' => array( $this, 'health_check_minimal' ),
			'permission_callback' => array( '\Care_API\Security\Security_Manager', 'check_api_permissions' )
		));

		// Version endpoint - SECURED WITH AUTHENTICATION
		register_rest_route( self::API_NAMESPACE, '/version', array(
			'methods' => 'GET',
			'callback' => array( $this, 'get_version' ),
			'permission_callback' => array( $this, 'check_admin_permissions' )
		));
	}

	/**
	 * Initialize WordPress integration
	 *
	 * @since 1.0.0
	 */
	public function init_wordpress_integration() {
		// Set up custom user roles
		$this->setup_user_roles();
		
		// Initialize custom database tables if needed
		$this->maybe_create_tables();
	}

	/**
	 * Initialize late loading components
	 *
	 * @since 1.0.0
	 */
	public function init_late_loading() {
		// Components that need WordPress to be fully loaded
	}

	/**
	 * Setup custom user roles
	 *
	 * @since 1.0.0
	 */
	private function setup_user_roles() {
		$roles_to_check = array( 'kivicare_doctor', 'kivicare_receptionist', 'kivicare_patient' );
		
		foreach ( $roles_to_check as $role ) {
			if ( ! get_role( $role ) ) {
				// Role doesn't exist, would be created during activation
			}
		}
	}

	/**
	 * Maybe create custom database tables
	 *
	 * @since 1.0.0
	 */
	private function maybe_create_tables() {
		$current_db_version = get_option( 'care_api_db_version', '0' );
		
		if ( version_compare( $current_db_version, self::VERSION, '<' ) ) {
			$this->create_database_tables();
			update_option( 'care_api_db_version', self::VERSION );
		}
	}

	/**
	 * Create database tables
	 *
	 * @since 1.0.0
	 */
	private function create_database_tables() {
		global $wpdb;
		
		$charset_collate = $wpdb->get_charset_collate();
		
		$tables = array();
		
		// API sessions table
		$tables[] = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}kc_api_sessions (
			id bigint(20) NOT NULL AUTO_INCREMENT,
			user_id bigint(20) NOT NULL,
			token_hash varchar(255) NOT NULL,
			expires_at datetime NOT NULL,
			created_at datetime DEFAULT CURRENT_TIMESTAMP,
			last_activity datetime DEFAULT CURRENT_TIMESTAMP,
			user_agent text,
			ip_address varchar(45),
			PRIMARY KEY (id),
			UNIQUE KEY token_hash (token_hash),
			KEY user_id (user_id),
			KEY expires_at (expires_at)
		) $charset_collate;";

		require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
		
		foreach ( $tables as $table_sql ) {
			dbDelta( $table_sql );
		}
	}

	/**
	 * Initialize admin area
	 *
	 * @since 1.0.0
	 */
	public function init_admin() {
		// Admin initialization code
	}

	/**
	 * Add admin menu
	 *
	 * @since 1.0.0
	 */
	public function add_admin_menu() {
		add_options_page(
			'Care API Settings',
			'Care API',
			'manage_options',
			'care-api-settings',
			array( $this, 'admin_page' )
		);
	}

	/**
	 * Admin page content
	 *
	 * @since 1.0.0
	 */
	public function admin_page() {
		echo '<div class="wrap">';
		echo '<h1>Care API Settings</h1>';
		echo '<p>Care API Version: ' . self::VERSION . '</p>';
		echo '<p>Status: Active</p>';
		echo '<p>Namespace: ' . self::API_NAMESPACE . '</p>';
		echo '</div>';
	}

	/**
	 * AJAX API status check
	 *
	 * @since 1.0.0
	 */
	public function ajax_api_status() {
		wp_send_json_success( array(
			'status' => 'active',
			'version' => self::VERSION
		));
	}

	/**
	 * Daily maintenance task
	 *
	 * @since 1.0.0
	 */
	public function daily_maintenance() {
		// Clean up expired sessions - FULLY SECURED
		global $wpdb;
		$table_name = $wpdb->prefix . 'kc_api_sessions';
		$wpdb->query(
			$wpdb->prepare(
				"DELETE FROM `{$table_name}` WHERE expires_at < %s",
				current_time( 'mysql' )
			)
		);

		// Clean up error logs
		if ( class_exists( 'Care_API\\Utils\\Error_Handler' ) ) {
			Utils\Error_Handler::clear_error_logs( 30 );
		}
	}

	/**
	 * REST API endpoint handlers
	 */

	/**
	 * Handle login
	 *
	 * @param \WP_REST_Request $request Request object
	 * @return \WP_REST_Response
	 */
	public function handle_login( $request ) {
		if ( class_exists( 'Care_API\\Services\\Auth_Service' ) ) {
			return Services\Auth_Service::login( $request );
		}
		
		return new \WP_REST_Response( array(
			'success' => false,
			'message' => 'Authentication service not available'
		), 503 );
	}

	/**
	 * Handle logout
	 *
	 * @param \WP_REST_Request $request Request object
	 * @return \WP_REST_Response
	 */
	public function handle_logout( $request ) {
		if ( class_exists( 'Care_API\\Services\\Auth_Service' ) ) {
			return Services\Auth_Service::logout( $request );
		}
		
		return new \WP_REST_Response( array(
			'success' => false,
			'message' => 'Authentication service not available'
		), 503 );
	}

	/**
	 * Get user profile
	 *
	 * @param \WP_REST_Request $request Request object
	 * @return \WP_REST_Response
	 */
	public function get_user_profile( $request ) {
		if ( class_exists( 'Care_API\\Services\\Auth_Service' ) ) {
			return Services\Auth_Service::get_profile( $request );
		}
		
		return new \WP_REST_Response( array(
			'success' => false,
			'message' => 'Authentication service not available'
		), 503 );
	}

	/**
	 * Check authentication
	 *
	 * @param \WP_REST_Request $request Request object
	 * @return bool|\WP_Error
	 */
	public function check_authentication( $request ) {
		if ( class_exists( 'Care_API\\Services\\Auth_Service' ) ) {
			return Services\Auth_Service::check_authentication( $request );
		}
		
		return new \WP_Error(
			'service_unavailable',
			'Authentication service not available',
			array( 'status' => 503 )
		);
	}

	/**
	 * Get API status
	 *
	 * @return \WP_REST_Response
	 * @since 1.0.0
	 */
	public function get_api_status() {
		global $wpdb;

		// Get basic Care database stats - SECURED WITH PREPARED STATEMENTS
		$clinic_count = $wpdb->get_var( $wpdb->prepare(
			"SELECT COUNT(*) FROM {$wpdb->prefix}kc_clinics WHERE status = %d",
			1
		));
		$patient_count = $wpdb->get_var( $wpdb->prepare(
			"SELECT COUNT(DISTINCT u.ID) FROM {$wpdb->users} u
			 INNER JOIN {$wpdb->usermeta} um ON u.ID = um.user_id
			 WHERE um.meta_key = %s
			 AND um.meta_value LIKE %s",
			$wpdb->prefix . 'capabilities',
			'%patient%'
		));

		$response_data = array(
			'status'         => 'active',
			'version'        => self::VERSION,
			'namespace'      => self::API_NAMESPACE,
			'timestamp'      => current_time( 'c' ),
			'wordpress_version' => get_bloginfo( 'version' ),
			'php_version'    => PHP_VERSION,
			'kivicare_active' => $this->is_kivicare_active(),
			'statistics'     => array(
				'active_clinics' => (int) $clinic_count,
				'total_patients' => (int) $patient_count,
			),
			'endpoints'      => $this->get_available_endpoints(),
		);

		return new \WP_REST_Response( $response_data, 200 );
	}

	/**
	 * Health check endpoint
	 *
	 * @return \WP_REST_Response
	 * @since 1.0.0
	 */
	public function health_check() {
		global $wpdb;
		
		$health = array(
			'status' => 'healthy',
			'checks' => array()
		);

		// Database connectivity check - SECURED
		try {
			$wpdb->get_var( $wpdb->prepare( "SELECT %d", 1 ) );
			$health['checks']['database'] = 'healthy';
		} catch ( Exception $e ) {
			$health['checks']['database'] = 'unhealthy';
			$health['status'] = 'unhealthy';
		}

		// Check if required tables exist
		$tables = array( 'kc_clinics', 'kc_appointments' );
		foreach ( $tables as $table ) {
			$table_exists = $wpdb->get_var( 
				$wpdb->prepare( "SHOW TABLES LIKE %s", $wpdb->prefix . $table ) 
			);
			$health['checks']["table_{$table}"] = $table_exists ? 'healthy' : 'missing';
			if ( ! $table_exists ) {
				$health['status'] = 'degraded';
			}
		}

		$status_code = $health['status'] === 'healthy' ? 200 : 503;
		
		return new \WP_REST_Response( $health, $status_code );
	}

	/**
	 * Get version information
	 *
	 * @return \WP_REST_Response
	 * @since 1.0.0
	 */
	public function get_version() {
		return new \WP_REST_Response( array(
			'version' => self::VERSION,
			'min_php_version' => self::MIN_PHP_VERSION,
			'min_wp_version' => self::MIN_WP_VERSION,
			'current_php_version' => PHP_VERSION,
			'current_wp_version' => get_bloginfo( 'version' )
		), 200 );
	}

	/**
	 * Health check with minimal data exposure
	 *
	 * @return \WP_REST_Response
	 * @since 1.0.0
	 */
	public function health_check_minimal() {
		$health = array(
			'status' => 'healthy',
			'timestamp' => current_time( 'c' ),
			'api_namespace' => self::API_NAMESPACE
		);

		// Only basic connectivity check - no sensitive data
		if ( ! $this->is_kivicare_active() ) {
			$health['status'] = 'degraded';
		}

		$status_code = $health['status'] === 'healthy' ? 200 : 503;
		return new \WP_REST_Response( $health, $status_code );
	}

	/**
	 * Check admin permissions for sensitive endpoints
	 *
	 * @return bool
	 * @since 1.0.0
	 */
	public function check_admin_permissions() {
		return current_user_can( 'manage_options' ) || $this->verify_jwt_token();
	}

	/**
	 * Verify JWT token for API access
	 *
	 * @return bool
	 * @since 1.0.0
	 */
	private function verify_jwt_token() {
		$auth_header = $_SERVER['HTTP_AUTHORIZATION'] ?? '';

		if ( empty( $auth_header ) || ! str_starts_with( $auth_header, 'Bearer ' ) ) {
			return false;
		}

		$token = substr( $auth_header, 7 );

		// Use Auth_Service for token validation
		if ( class_exists( 'Care_API\Auth_Service' ) ) {
			return Auth_Service::verify_token( $token );
		}

		return false;
	}

	/**
	 * Get list of available API endpoints.
	 *
	 * @return array
	 */
	private function get_available_endpoints() {
		return array(
			'authentication' => array(
				'POST /auth/login',
				'POST /auth/logout',
				'GET /auth/profile',
			),
			'clinics'        => array(
				'GET /clinics',
				'POST /clinics',
				'GET /clinics/{id}',
				'PUT /clinics/{id}',
				'DELETE /clinics/{id}',
				'GET /clinics/search',
				'GET /clinics/{id}/dashboard',
			),
			'patients'       => array(
				'POST /patients',
				'GET /patients/{id}',
				'PUT /patients/{id}',
				'GET /patients/search',
				'GET /patients/{id}/dashboard',
				'GET /patients/{id}/history',
			),
			'doctors'        => array(
				'GET /doctors',
				'POST /doctors',
				'GET /doctors/{id}',
				'PUT /doctors/{id}',
				'DELETE /doctors/{id}',
				'GET /doctors/search',
				'GET /doctors/{id}/schedule',
				'PUT /doctors/{id}/schedule',
				'GET /doctors/{id}/stats',
				'POST /doctors/bulk',
			),
			'appointments'   => array(
				'GET /appointments',
				'POST /appointments',
				'GET /appointments/{id}',
				'PUT /appointments/{id}',
				'POST /appointments/{id}/cancel',
				'POST /appointments/{id}/complete',
				'GET /appointments/availability/{doctor_id}',
				'GET /appointments/search',
				'POST /appointments/bulk',
			),
			'encounters'     => array(
				'GET /encounters',
				'POST /encounters',
				'GET /encounters/{id}',
				'PUT /encounters/{id}',
				'DELETE /encounters/{id}',
				'POST /encounters/{id}/start',
				'POST /encounters/{id}/complete',
				'GET /encounters/{id}/soap',
				'PUT /encounters/{id}/soap',
				'GET /encounters/{id}/vitals',
				'PUT /encounters/{id}/vitals',
				'GET /encounters/search',
				'GET /encounters/templates',
			),
			'prescriptions'  => array(
				'GET /prescriptions',
				'POST /prescriptions',
				'GET /prescriptions/{id}',
				'PUT /prescriptions/{id}',
				'DELETE /prescriptions/{id}',
				'POST /prescriptions/{id}/renew',
				'POST /prescriptions/check-interactions',
				'GET /prescriptions/patient/{patient_id}',
				'GET /prescriptions/patient/{patient_id}/active',
				'GET /prescriptions/search',
				'GET /prescriptions/stats',
				'POST /prescriptions/bulk',
			),
			'bills'          => array(
				'GET /bills',
				'POST /bills',
				'GET /bills/{id}',
				'PUT /bills/{id}',
				'DELETE /bills/{id}',
				'POST /bills/{id}/finalize',
				'POST /bills/{id}/payments',
				'GET /bills/{id}/payments',
				'GET /bills/patient/{patient_id}',
				'GET /bills/overdue',
				'POST /bills/{id}/remind',
				'GET /bills/search',
				'GET /bills/stats',
				'POST /bills/bulk',
			),
			'utilities'      => array(
				'GET /status',
				'GET /health',
				'GET /version',
			),
		);
	}

	/**
	 * Modify REST API response headers.
	 *
	 * @param bool                      $served  Whether the request has already been served.
	 * @param \WP_HTTP_Response         $result  Result to send to the client.
	 * @param \WP_REST_Request          $request Request used to generate the response.
	 * @param \WP_REST_Server           $server  Server instance.
	 * @return bool
	 */
	public function rest_pre_serve_request( $served, $result, $request, $server ) {
		// Only modify responses for our API namespace
		$route = $request->get_route();
		if ( strpos( $route, '/' . self::API_NAMESPACE . '/' ) !== 0 ) {
			return $served;
		}

		// Add custom headers
		$result->header( 'X-Care-API-Version', self::VERSION );
		$result->header( 'X-Powered-By', 'Care API by Descomplicar®' );

		// Add CORS headers for development
		if ( defined( 'CARE_API_DEBUG' ) && CARE_API_DEBUG ) {
			$result->header( 'Access-Control-Allow-Origin', '*' );
			$result->header( 'Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS' );
			$result->header( 'Access-Control-Allow-Headers', 'Authorization, Content-Type, X-WP-Nonce' );
		}

		return $served;
	}

	/**
	 * Get the API namespace.
	 *
	 * @return string
	 */
	public static function get_namespace() {
		return self::API_NAMESPACE;
	}

	/**
	 * Get the API version string.
	 *
	 * @return string
	 */
	public static function get_api_version() {
		return self::VERSION;
	}
}