<?php
/**
 * Prescription REST API Endpoints
 *
 * @package Care_API
 */

namespace Care_API\Endpoints;

use WP_REST_Server;
use WP_REST_Request;
use WP_REST_Response;
use WP_Error;
use Care_API\Services\Database\Prescription_Service;
use Care_API\Services\Permission_Service;
use Care_API\Utils\Input_Validator;
use Care_API\Utils\Error_Handler;

/**
 * Prescription Endpoints Class
 */
class Prescription_Endpoints {

	/**
	 * API namespace
	 *
	 * @var string
	 */
	private const NAMESPACE = 'care/v1';

	/**
	 * Register prescription REST routes.
	 */
	public static function register_routes() {
		// Get all prescriptions
		register_rest_route( self::NAMESPACE, '/prescriptions', array(
			'methods'             => WP_REST_Server::READABLE,
			'callback'            => array( __CLASS__, 'get_prescriptions' ),
			'permission_callback' => array( __CLASS__, 'check_read_permission' ),
			'args'                => array(
				'page' => array(
					'description'       => 'Page number for pagination',
					'type'              => 'integer',
					'default'           => 1,
					'minimum'           => 1,
					'sanitize_callback' => 'absint',
				),
				'per_page' => array(
					'description'       => 'Number of prescriptions per page',
					'type'              => 'integer',
					'default'           => 10,
					'minimum'           => 1,
					'maximum'           => 100,
					'sanitize_callback' => 'absint',
				),
				'status' => array(
					'description' => 'Filter by prescription status',
					'type'        => 'string',
					'enum'        => array( 'active', 'completed', 'cancelled', 'expired' ),
				),
				'patient_id' => array(
					'description'       => 'Filter by patient ID',
					'type'              => 'integer',
					'sanitize_callback' => 'absint',
				),
				'doctor_id' => array(
					'description'       => 'Filter by doctor ID',
					'type'              => 'integer',
					'sanitize_callback' => 'absint',
				),
				'encounter_id' => array(
					'description'       => 'Filter by encounter ID',
					'type'              => 'integer',
					'sanitize_callback' => 'absint',
				),
				'date_from' => array(
					'description' => 'Filter prescriptions from date (YYYY-MM-DD)',
					'type'        => 'string',
					'format'      => 'date',
				),
				'date_to' => array(
					'description' => 'Filter prescriptions to date (YYYY-MM-DD)',
					'type'        => 'string',
					'format'      => 'date',
				),
			),
		));

		// Create new prescription
		register_rest_route( self::NAMESPACE, '/prescriptions', array(
			'methods'             => WP_REST_Server::CREATABLE,
			'callback'            => array( __CLASS__, 'create_prescription' ),
			'permission_callback' => array( __CLASS__, 'check_create_permission' ),
			'args'                => array(
				'patient_id' => array(
					'description'       => 'Patient ID',
					'type'              => 'integer',
					'required'          => true,
					'sanitize_callback' => 'absint',
				),
				'doctor_id' => array(
					'description'       => 'Doctor ID',
					'type'              => 'integer',
					'required'          => true,
					'sanitize_callback' => 'absint',
				),
				'encounter_id' => array(
					'description'       => 'Related encounter ID',
					'type'              => 'integer',
					'sanitize_callback' => 'absint',
				),
				'prescription_date' => array(
					'description' => 'Prescription date (YYYY-MM-DD)',
					'type'        => 'string',
					'required'    => true,
					'format'      => 'date',
				),
				'medications' => array(
					'description' => 'Array of medication objects',
					'type'        => 'array',
					'required'    => true,
					'items'       => array(
						'type' => 'object',
					),
					'minItems' => 1,
				),
				'instructions' => array(
					'description'       => 'General prescription instructions',
					'type'              => 'string',
					'sanitize_callback' => 'sanitize_textarea_field',
				),
				'diagnosis_codes' => array(
					'description' => 'Associated diagnosis codes',
					'type'        => 'array',
					'items'       => array(
						'type' => 'string',
					),
				),
				'valid_until' => array(
					'description' => 'Prescription validity end date (YYYY-MM-DD)',
					'type'        => 'string',
					'format'      => 'date',
				),
			),
		));

		// Get specific prescription
		register_rest_route( self::NAMESPACE, '/prescriptions/(?P<id>\d+)', array(
			'methods'             => WP_REST_Server::READABLE,
			'callback'            => array( __CLASS__, 'get_prescription' ),
			'permission_callback' => array( __CLASS__, 'check_read_permission' ),
			'args'                => array(
				'id' => array(
					'description'       => 'Prescription ID',
					'type'              => 'integer',
					'required'          => true,
					'sanitize_callback' => 'absint',
				),
			),
		));

		// Update prescription
		register_rest_route( self::NAMESPACE, '/prescriptions/(?P<id>\d+)', array(
			'methods'             => WP_REST_Server::EDITABLE,
			'callback'            => array( __CLASS__, 'update_prescription' ),
			'permission_callback' => array( __CLASS__, 'check_update_permission' ),
			'args'                => array(
				'id' => array(
					'description'       => 'Prescription ID',
					'type'              => 'integer',
					'required'          => true,
					'sanitize_callback' => 'absint',
				),
				'medications' => array(
					'description' => 'Array of medication objects',
					'type'        => 'array',
					'items'       => array(
						'type' => 'object',
					),
				),
				'instructions' => array(
					'description'       => 'General prescription instructions',
					'type'              => 'string',
					'sanitize_callback' => 'sanitize_textarea_field',
				),
				'diagnosis_codes' => array(
					'description' => 'Associated diagnosis codes',
					'type'        => 'array',
					'items'       => array(
						'type' => 'string',
					),
				),
				'valid_until' => array(
					'description' => 'Prescription validity end date (YYYY-MM-DD)',
					'type'        => 'string',
					'format'      => 'date',
				),
				'status' => array(
					'description' => 'Prescription status',
					'type'        => 'string',
					'enum'        => array( 'active', 'completed', 'cancelled', 'expired' ),
				),
			),
		));

		// Delete prescription
		register_rest_route( self::NAMESPACE, '/prescriptions/(?P<id>\d+)', array(
			'methods'             => WP_REST_Server::DELETABLE,
			'callback'            => array( __CLASS__, 'delete_prescription' ),
			'permission_callback' => array( __CLASS__, 'check_delete_permission' ),
			'args'                => array(
				'id' => array(
					'description'       => 'Prescription ID',
					'type'              => 'integer',
					'required'          => true,
					'sanitize_callback' => 'absint',
				),
				'reason' => array(
					'description'       => 'Reason for cancellation',
					'type'              => 'string',
					'sanitize_callback' => 'sanitize_textarea_field',
				),
			),
		));

		// Renew prescription
		register_rest_route( self::NAMESPACE, '/prescriptions/(?P<id>\d+)/renew', array(
			'methods'             => WP_REST_Server::CREATABLE,
			'callback'            => array( __CLASS__, 'renew_prescription' ),
			'permission_callback' => array( __CLASS__, 'check_create_permission' ),
			'args'                => array(
				'id' => array(
					'description'       => 'Prescription ID',
					'type'              => 'integer',
					'required'          => true,
					'sanitize_callback' => 'absint',
				),
				'renewal_date' => array(
					'description' => 'Renewal date (YYYY-MM-DD)',
					'type'        => 'string',
					'format'      => 'date',
				),
				'valid_until' => array(
					'description' => 'New validity end date (YYYY-MM-DD)',
					'type'        => 'string',
					'format'      => 'date',
				),
				'modifications' => array(
					'description' => 'Modifications to the prescription',
					'type'        => 'object',
				),
				'renewal_notes' => array(
					'description'       => 'Notes about the renewal',
					'type'              => 'string',
					'sanitize_callback' => 'sanitize_textarea_field',
				),
			),
		));

		// Check drug interactions
		register_rest_route( self::NAMESPACE, '/prescriptions/check-interactions', array(
			'methods'             => WP_REST_Server::CREATABLE,
			'callback'            => array( __CLASS__, 'check_drug_interactions' ),
			'permission_callback' => array( __CLASS__, 'check_read_permission' ),
			'args'                => array(
				'patient_id' => array(
					'description'       => 'Patient ID',
					'type'              => 'integer',
					'required'          => true,
					'sanitize_callback' => 'absint',
				),
				'medications' => array(
					'description' => 'Array of medications to check',
					'type'        => 'array',
					'required'    => true,
					'items'       => array(
						'type' => 'object',
					),
					'minItems' => 1,
				),
			),
		));

		// Get prescription history for patient
		register_rest_route( self::NAMESPACE, '/prescriptions/patient/(?P<patient_id>\d+)', array(
			'methods'             => WP_REST_Server::READABLE,
			'callback'            => array( __CLASS__, 'get_patient_prescription_history' ),
			'permission_callback' => array( __CLASS__, 'check_read_permission' ),
			'args'                => array(
				'patient_id' => array(
					'description'       => 'Patient ID',
					'type'              => 'integer',
					'required'          => true,
					'sanitize_callback' => 'absint',
				),
				'status' => array(
					'description' => 'Filter by status',
					'type'        => 'string',
					'enum'        => array( 'active', 'completed', 'cancelled', 'expired' ),
				),
				'limit' => array(
					'description'       => 'Number of records to return',
					'type'              => 'integer',
					'default'           => 20,
					'minimum'           => 1,
					'maximum'           => 100,
					'sanitize_callback' => 'absint',
				),
			),
		));

		// Get active prescriptions for patient
		register_rest_route( self::NAMESPACE, '/prescriptions/patient/(?P<patient_id>\d+)/active', array(
			'methods'             => WP_REST_Server::READABLE,
			'callback'            => array( __CLASS__, 'get_patient_active_prescriptions' ),
			'permission_callback' => array( __CLASS__, 'check_read_permission' ),
			'args'                => array(
				'patient_id' => array(
					'description'       => 'Patient ID',
					'type'              => 'integer',
					'required'          => true,
					'sanitize_callback' => 'absint',
				),
			),
		));

		// Search prescriptions
		register_rest_route( self::NAMESPACE, '/prescriptions/search', array(
			'methods'             => WP_REST_Server::READABLE,
			'callback'            => array( __CLASS__, 'search_prescriptions' ),
			'permission_callback' => array( __CLASS__, 'check_read_permission' ),
			'args'                => array(
				'q' => array(
					'description'       => 'Search query',
					'type'              => 'string',
					'required'          => true,
					'sanitize_callback' => 'sanitize_text_field',
				),
				'fields' => array(
					'description' => 'Fields to search in',
					'type'        => 'array',
					'items'       => array(
						'type' => 'string',
						'enum' => array( 'patient_name', 'doctor_name', 'medication_name', 'diagnosis' ),
					),
					'default' => array( 'patient_name', 'medication_name' ),
				),
				'limit' => array(
					'description'       => 'Maximum results to return',
					'type'              => 'integer',
					'default'           => 10,
					'minimum'           => 1,
					'maximum'           => 50,
					'sanitize_callback' => 'absint',
				),
			),
		));

		// Get prescription statistics
		register_rest_route( self::NAMESPACE, '/prescriptions/stats', array(
			'methods'             => WP_REST_Server::READABLE,
			'callback'            => array( __CLASS__, 'get_prescription_statistics' ),
			'permission_callback' => array( __CLASS__, 'check_read_permission' ),
			'args'                => array(
				'period' => array(
					'description' => 'Statistics period',
					'type'        => 'string',
					'enum'        => array( 'week', 'month', 'quarter', 'year' ),
					'default'     => 'month',
				),
				'doctor_id' => array(
					'description'       => 'Filter by doctor ID',
					'type'              => 'integer',
					'sanitize_callback' => 'absint',
				),
			),
		));

		// Bulk operations
		register_rest_route( self::NAMESPACE, '/prescriptions/bulk', array(
			'methods'             => WP_REST_Server::CREATABLE,
			'callback'            => array( __CLASS__, 'bulk_operations' ),
			'permission_callback' => array( __CLASS__, 'check_update_permission' ),
			'args'                => array(
				'action' => array(
					'description' => 'Bulk action to perform',
					'type'        => 'string',
					'required'    => true,
					'enum'        => array( 'cancel', 'expire', 'reactivate' ),
				),
				'prescription_ids' => array(
					'description' => 'Array of prescription IDs',
					'type'        => 'array',
					'required'    => true,
					'items'       => array(
						'type' => 'integer',
					),
					'minItems' => 1,
				),
				'reason' => array(
					'description'       => 'Reason for the bulk action',
					'type'              => 'string',
					'sanitize_callback' => 'sanitize_textarea_field',
				),
			),
		));
	}

	/**
	 * Get prescriptions list.
	 */
	public static function get_prescriptions( WP_REST_Request $request ) {
		try {
			$page     = $request->get_param( 'page' );
			$per_page = $request->get_param( 'per_page' );
			$offset   = ( $page - 1 ) * $per_page;

			$args = array(
				'limit'  => $per_page,
				'offset' => $offset,
			);

			// Add filters
			$filters = array( 'status', 'patient_id', 'doctor_id', 'encounter_id', 'date_from', 'date_to' );
			foreach ( $filters as $filter ) {
				$value = $request->get_param( $filter );
				if ( $value ) {
					$args[$filter] = $value;
				}
			}

			$result = Prescription_Service::get_prescriptions( $args );

			if ( is_wp_error( $result ) ) {
				return Error_Handler::handle_service_error( $result );
			}

			$total_prescriptions = Prescription_Service::get_prescriptions_count( $args );
			$total_pages         = ceil( $total_prescriptions / $per_page );

			$response = new WP_REST_Response( array(
				'success' => true,
				'data'    => $result,
				'meta'    => array(
					'total'       => $total_prescriptions,
					'pages'       => $total_pages,
					'current'     => $page,
					'per_page'    => $per_page,
				),
			), 200 );

			return $response;

		} catch ( Exception $e ) {
			return Error_Handler::handle_exception( $e );
		}
	}

	/**
	 * Create new prescription.
	 */
	public static function create_prescription( WP_REST_Request $request ) {
		try {
			$prescription_data = array(
				'patient_id'        => $request->get_param( 'patient_id' ),
				'doctor_id'         => $request->get_param( 'doctor_id' ),
				'encounter_id'      => $request->get_param( 'encounter_id' ),
				'prescription_date' => $request->get_param( 'prescription_date' ),
				'medications'       => $request->get_param( 'medications' ),
				'instructions'      => $request->get_param( 'instructions' ),
				'diagnosis_codes'   => $request->get_param( 'diagnosis_codes' ),
				'valid_until'       => $request->get_param( 'valid_until' ),
			);

			// Validate input data
			$validation_result = Input_Validator::validate_prescription_data( $prescription_data );
			if ( is_wp_error( $validation_result ) ) {
				return Error_Handler::handle_service_error( $validation_result );
			}

			$result = Prescription_Service::create_prescription( $prescription_data );

			if ( is_wp_error( $result ) ) {
				return Error_Handler::handle_service_error( $result );
			}

			return new WP_REST_Response( array(
				'success' => true,
				'message' => 'Prescription created successfully',
				'data'    => $result,
			), 201 );

		} catch ( Exception $e ) {
			return Error_Handler::handle_exception( $e );
		}
	}

	/**
	 * Get specific prescription.
	 */
	public static function get_prescription( WP_REST_Request $request ) {
		try {
			$prescription_id = $request->get_param( 'id' );
			$result          = Prescription_Service::get_prescription( $prescription_id );

			if ( is_wp_error( $result ) ) {
				return Error_Handler::handle_service_error( $result );
			}

			return new WP_REST_Response( array(
				'success' => true,
				'data'    => $result,
			), 200 );

		} catch ( Exception $e ) {
			return Error_Handler::handle_exception( $e );
		}
	}

	/**
	 * Update prescription.
	 */
	public static function update_prescription( WP_REST_Request $request ) {
		try {
			$prescription_id = $request->get_param( 'id' );
			$update_data     = array();

			// Only include parameters that were actually sent
			$params = array(
				'medications', 'instructions', 'diagnosis_codes',
				'valid_until', 'status'
			);

			foreach ( $params as $param ) {
				if ( $request->has_param( $param ) ) {
					$update_data[$param] = $request->get_param( $param );
				}
			}

			if ( empty( $update_data ) ) {
				return new WP_REST_Response( array(
					'success' => false,
					'message' => 'No data provided for update',
				), 400 );
			}

			// Validate input data
			$validation_result = Input_Validator::validate_prescription_data( $update_data, true );
			if ( is_wp_error( $validation_result ) ) {
				return Error_Handler::handle_service_error( $validation_result );
			}

			$result = Prescription_Service::update_prescription( $prescription_id, $update_data );

			if ( is_wp_error( $result ) ) {
				return Error_Handler::handle_service_error( $result );
			}

			return new WP_REST_Response( array(
				'success' => true,
				'message' => 'Prescription updated successfully',
				'data'    => $result,
			), 200 );

		} catch ( Exception $e ) {
			return Error_Handler::handle_exception( $e );
		}
	}

	/**
	 * Delete/Cancel prescription.
	 */
	public static function delete_prescription( WP_REST_Request $request ) {
		try {
			$prescription_id = $request->get_param( 'id' );
			$reason          = $request->get_param( 'reason' );

			$result = Prescription_Service::cancel_prescription( $prescription_id, $reason );

			if ( is_wp_error( $result ) ) {
				return Error_Handler::handle_service_error( $result );
			}

			return new WP_REST_Response( array(
				'success' => true,
				'message' => 'Prescription cancelled successfully',
				'data'    => $result,
			), 200 );

		} catch ( Exception $e ) {
			return Error_Handler::handle_exception( $e );
		}
	}

	/**
	 * Renew prescription.
	 */
	public static function renew_prescription( WP_REST_Request $request ) {
		try {
			$prescription_id = $request->get_param( 'id' );
			$renewal_data    = array(
				'renewal_date'   => $request->get_param( 'renewal_date' ),
				'valid_until'    => $request->get_param( 'valid_until' ),
				'modifications'  => $request->get_param( 'modifications' ),
				'renewal_notes'  => $request->get_param( 'renewal_notes' ),
			);

			$result = Prescription_Service::renew_prescription( $prescription_id, $renewal_data );

			if ( is_wp_error( $result ) ) {
				return Error_Handler::handle_service_error( $result );
			}

			return new WP_REST_Response( array(
				'success' => true,
				'message' => 'Prescription renewed successfully',
				'data'    => $result,
			), 201 );

		} catch ( Exception $e ) {
			return Error_Handler::handle_exception( $e );
		}
	}

	/**
	 * Check drug interactions.
	 */
	public static function check_drug_interactions( WP_REST_Request $request ) {
		try {
			$patient_id   = $request->get_param( 'patient_id' );
			$medications  = $request->get_param( 'medications' );

			$result = Prescription_Service::check_drug_interactions( $patient_id, $medications );

			if ( is_wp_error( $result ) ) {
				return Error_Handler::handle_service_error( $result );
			}

			return new WP_REST_Response( array(
				'success' => true,
				'data'    => $result,
			), 200 );

		} catch ( Exception $e ) {
			return Error_Handler::handle_exception( $e );
		}
	}

	/**
	 * Get patient prescription history.
	 */
	public static function get_patient_prescription_history( WP_REST_Request $request ) {
		try {
			$patient_id = $request->get_param( 'patient_id' );
			$status     = $request->get_param( 'status' );
			$limit      = $request->get_param( 'limit' );

			$result = Prescription_Service::get_patient_prescription_history( $patient_id, $status, $limit );

			if ( is_wp_error( $result ) ) {
				return Error_Handler::handle_service_error( $result );
			}

			return new WP_REST_Response( array(
				'success' => true,
				'data'    => $result,
			), 200 );

		} catch ( Exception $e ) {
			return Error_Handler::handle_exception( $e );
		}
	}

	/**
	 * Get patient active prescriptions.
	 */
	public static function get_patient_active_prescriptions( WP_REST_Request $request ) {
		try {
			$patient_id = $request->get_param( 'patient_id' );
			$result     = Prescription_Service::get_active_prescriptions( $patient_id );

			if ( is_wp_error( $result ) ) {
				return Error_Handler::handle_service_error( $result );
			}

			return new WP_REST_Response( array(
				'success' => true,
				'data'    => $result,
			), 200 );

		} catch ( Exception $e ) {
			return Error_Handler::handle_exception( $e );
		}
	}

	/**
	 * Search prescriptions.
	 */
	public static function search_prescriptions( WP_REST_Request $request ) {
		try {
			$query  = $request->get_param( 'q' );
			$fields = $request->get_param( 'fields' );
			$limit  = $request->get_param( 'limit' );

			$result = Prescription_Service::search_prescriptions( $query, $fields, $limit );

			if ( is_wp_error( $result ) ) {
				return Error_Handler::handle_service_error( $result );
			}

			return new WP_REST_Response( array(
				'success' => true,
				'data'    => $result,
				'meta'    => array(
					'query'   => $query,
					'fields'  => $fields,
					'results' => count( $result ),
				),
			), 200 );

		} catch ( Exception $e ) {
			return Error_Handler::handle_exception( $e );
		}
	}

	/**
	 * Get prescription statistics.
	 */
	public static function get_prescription_statistics( WP_REST_Request $request ) {
		try {
			$period    = $request->get_param( 'period' );
			$doctor_id = $request->get_param( 'doctor_id' );

			$result = Prescription_Service::get_prescription_statistics( $period, $doctor_id );

			if ( is_wp_error( $result ) ) {
				return Error_Handler::handle_service_error( $result );
			}

			return new WP_REST_Response( array(
				'success' => true,
				'data'    => $result,
			), 200 );

		} catch ( Exception $e ) {
			return Error_Handler::handle_exception( $e );
		}
	}

	/**
	 * Handle bulk operations.
	 */
	public static function bulk_operations( WP_REST_Request $request ) {
		try {
			$action           = $request->get_param( 'action' );
			$prescription_ids = $request->get_param( 'prescription_ids' );
			$reason           = $request->get_param( 'reason' );

			$result = Prescription_Service::bulk_update_prescriptions( $prescription_ids, $action, $reason );

			if ( is_wp_error( $result ) ) {
				return Error_Handler::handle_service_error( $result );
			}

			return new WP_REST_Response( array(
				'success' => true,
				'message' => sprintf( 'Bulk operation "%s" completed successfully', $action ),
				'data'    => $result,
			), 200 );

		} catch ( Exception $e ) {
			return Error_Handler::handle_exception( $e );
		}
	}

	/**
	 * Check read permission.
	 */
	public static function check_read_permission( WP_REST_Request $request ) {
		return Permission_Service::can_read_prescriptions();
	}

	/**
	 * Check create permission.
	 */
	public static function check_create_permission( WP_REST_Request $request ) {
		return Permission_Service::can_manage_prescriptions();
	}

	/**
	 * Check update permission.
	 */
	public static function check_update_permission( WP_REST_Request $request ) {
		$prescription_id = $request->get_param( 'id' );
		return Permission_Service::can_edit_prescription( $prescription_id );
	}

	/**
	 * Check delete permission.
	 */
	public static function check_delete_permission( WP_REST_Request $request ) {
		$prescription_id = $request->get_param( 'id' );
		return Permission_Service::can_delete_prescription( $prescription_id );
	}
}