<?php
namespace Vidplex;

// If this file is called directly, abort.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class Rest_API
 *
 * Handles all custom REST API endpoints for the plugin.
 * Responsible for registering routes, processing requests,
 * and returning responses related to channels, videos, and
 * other plugin-specific data.
 */
class Rest_API {
	/**
	 * Singleton instance of the class.
	 *
	 * @var self|null
	 */
	private static $instance = null;

	/**
	 * Returns the singleton instance of the class.
	 *
	 * Ensures that only one instance of the class is created and reused
	 * throughout the request lifecycle.
	 *
	 * @return self
	 */
	public static function get_instance() {
		if ( is_null( self::$instance ) ) {
			self::$instance = new self();
		}

		return self::$instance;
	}

	/**
	 * Class constructor.
	 *
	 * Registers necessary REST API route registration.
	 *
	 * @return void
	 */
	private function __construct() {
		add_action( 'rest_api_init', array( $this, 'register_plugin_routes' ) );
	}

	/**
	 * Registers custom REST API routes for the plugin.
	 *
	 * Defines endpoints for managing channels, videos, or other
	 * plugin-specific data via the WordPress REST API.
	 *
	 * @return void
	 */
	public function register_plugin_routes() {
		register_rest_route(
			'vidplex/v1',
			'/videos',
			array(
				'methods'             => \WP_REST_Server::READABLE,
				'callback'            => array( $this, 'get_videos' ),
				'permission_callback' => '__return_true',
			)
		);

		register_rest_route(
			'vidplex/v1',
			'/channel-details',
			array(
				'methods'             => \WP_REST_Server::READABLE,
				'callback'            => array( $this, 'get_channel_details' ),
				'permission_callback' => '__return_true',
			)
		);
	}

	/**
	 * Handles the REST API request to retrieve videos.
	 *
	 * Processes the incoming WP_REST_Request and returns a list of
	 * videos, optionally filtered by request parameters.
	 *
	 * @param \WP_REST_Request $request The REST API request object containing parameters.
	 * @return \WP_REST_Response Response containing the video data.
	 */
	public function get_videos( \WP_REST_Request $request ) {
		$source   = $request->get_param( 'source' );
		$id       = $request->get_param( 'id' );
		$order    = $request->get_param( 'order' );
		$order_by = $request->get_param( 'orderBy' );

		if ( empty( $source ) || empty( $id ) ) {
			return array();
		}

		switch ( $source ) {
			case 'channel':
				return \Vidplex\Helpers::video_filters(
					\Vidplex\Store::get_instance()->videos->get_videos_by_channel( $id ),
					array(
						'order'   => $order,
						'orderBy' => $order_by,
					)
				);

			case 'playlist':
				break;

			default:
				return array();
		}
	}

	/**
	 * Handles the REST API request to retrieve channel details.
	 *
	 * Processes the incoming WP_REST_Request and returns detailed
	 * information about a specific channel.
	 *
	 * @param \WP_REST_Request $request The REST API request object containing parameters.
	 * @return \WP_REST_Response Response containing the channel details.
	 */
	public function get_channel_details( \WP_REST_Request $request ) {
		$id     = $request->get_param( 'id' );
		$format = $request->get_param( 'format' );

		if ( empty( $id ) ) {
			return array();
		}

		$channel_data = \Vidplex\Store::get_instance()->channels->get_channel_by_id( $id );

		$additional_data                      = \Vidplex\Store::get_instance()->channels->get_single_fields( $id, array( 'statistics', 'branding_settings', 'thumbnail_blob', 'mime_type', 'banner_blob', 'banner_mime_type' ) );
		$channel_data[0]['statistics']        = $additional_data[0]['statistics'];
		$channel_data[0]['branding_settings'] = $additional_data[0]['branding_settings'];
		$channel_data[0]['thumbnail_blob']    = base64_encode( $additional_data[0]['thumbnail_blob'] );
		$channel_data[0]['mime_type']         = $additional_data[0]['mime_type'];
		$channel_data[0]['banner_blob']       = base64_encode( $additional_data[0]['banner_blob'] );
		$channel_data[0]['banner_mime_type']  = $additional_data[0]['banner_mime_type'];

		if ( isset( $format ) || empty( $format ) ) {

			return $channel_data;
		}

		if ( 'json' === $format ) {
			wp_send_json_success(
				array(
					'data' => $channel_data,
				)
			);
		}
	}
}
