<?php
/**
 * Font families REST API controller.
 *
 * @package PDFDraft
 */

namespace PDFDraft\Controllers\V1;

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

use PDFDraft\Contracts\Services\FontsServiceInterface;

/**
 * Handles REST API requests for font families.
 */
class FontFamiliesController extends \WP_REST_Controller {

	/**
	 * API namespace.
	 *
	 * @var string
	 */
	protected $namespace = 'pdfdraft/v1';

	/**
	 * REST base route.
	 *
	 * @var string
	 */
	protected $rest_base = 'font-families';

	/**
	 * Fonts service instance.
	 *
	 * @var FontsServiceInterface
	 */
	private FontsServiceInterface $fontsService;

	/**
	 * Constructor.
	 *
	 * @param FontsServiceInterface $fontsService Fonts service dependency.
	 */
	public function __construct( FontsServiceInterface $fontsService ) {
		$this->fontsService = $fontsService;
		$this->register_routes();
	}

	/**
	 * Register REST API routes for font families.
	 *
	 * @return void
	 */
	public function register_routes() {
		register_rest_route(
			$this->namespace,
			'/' . $this->rest_base,
			array(
				array(
					'methods'             => \WP_REST_Server::READABLE,
					'callback'            => array( $this, 'get_items' ),
					'permission_callback' => array( $this, 'get_items_permissions_check' ),
					'args'                => array(
						'query' => array(
							'type'     => 'string',
							'required' => false,
						),
						'limit' => array(
							'type'     => 'number',
							'required' => false,
							'default'  => 30,
						),
					),
				),
			)
		);
		// Single font family by string ID.
		register_rest_route(
			$this->namespace,
			'/' . $this->rest_base . '/(?P<id>[\w-]+)',
			array(
				array(
					'methods'             => \WP_REST_Server::READABLE,
					'callback'            => array( $this, 'get_item' ),
					'permission_callback' => array( $this, 'get_item_permissions_check' ),
					'args'                => array(
						'id' => array(
							'type'     => 'string',
							'required' => true,
						),
					),
				),
			)
		);
	}

	/**
	 * Check permissions for retrieving font families.
	 *
	 * @param \WP_REST_Request $request Full details about the request.
	 * @return bool
	 */
	public function get_items_permissions_check( $request ) {
		return true;
	}

	/**
	 * Check permissions for retrieving a single font family.
	 *
	 * @param \WP_REST_Request $request Full details about the request.
	 * @return bool
	 */
	public function get_item_permissions_check( $request ) {
		return true;
	}

	/**
	 * Retrieve a collection of font families.
	 *
	 * @param \WP_REST_Request $request Full details about the request.
	 * @return \WP_REST_Response|\WP_Error Response object or error.
	 */
	public function get_items( $request ) {
		$fonts = $this->fontsService->getFonts( $request->get_query_params() );
		if ( is_wp_error( $fonts ) ) {
			return $fonts;
		}
		// TODO: Add pagination support.
		return rest_ensure_response( $fonts );
	}

	/**
	 * Retrieve a single font family.
	 *
	 * @param \WP_REST_Request $request Full details about the request.
	 * @return \WP_REST_Response Response object.
	 */
	public function get_item( $request ) {
		return rest_ensure_response( $this->fontsService->getFontById( $request['id'] ?? '' ) );
	}
}
