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

namespace PDFDraft\Controllers\V1;

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

use PDFDraft\Settings;
use PDFDraft\Traits\Hooks;

/**
 * Handles PDFDraft settings via the REST API.
 */
class SettingsController extends \WP_REST_Settings_Controller {

	use Hooks;

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

	/**
	 * REST API base route.
	 *
	 * @var string
	 */
	protected $rest_base = 'settings';

	/**
	 * Get the registered settings options.
	 *
	 * @return array Registered options configuration.
	 */
	protected function get_registered_options() {
		$registered_options = array(
			'_pdfdraft_settings' => array(
				'name'        => __( 'PDFDraft Settings', 'pdfdraft' ),
				'schema'      => array(
					'type'        => 'object',
					'title'       => __( 'PDFDraft Settings', 'pdfdraft' ),
					'description' => __( 'Settings for PDFDraft.', 'pdfdraft' ),
					'default'     => pdfdraft( Settings::class )->getDefaults(),
					'properties'  => $this->applyFilters(
						// phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
						'pdfdraft:settings-schema:properties',
						array(
							'version'      => array(
								'type'        => 'string',
								'description' => __( 'The version of PDFDraft.', 'pdfdraft' ),
								'default'     => null,
								'readonly'    => true,
							),
							'install_time' => array(
								'type'        => 'string',
								'description' => __( 'The time PDFDraft was installed.', 'pdfdraft' ),
								'default'     => null,
								'readonly'    => true,
							),
							'debug_mode'   => array(
								'type'        => 'boolean',
								'description' => __( 'Whether debug mode is enabled.', 'pdfdraft' ),
								'default'     => false,
							),
							'last_updated' => array(
								'type'        => 'string',
								'description' => __( 'The last time the settings were updated.', 'pdfdraft' ),
								'default'     => null,
								'readonly'    => true,
							),
						)
					),
				),
				'option_name' => '_pdfdraft_settings',
			),
		);

		// phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
		return $this->applyFilters( 'pdfdraft:settings:registered-options', $registered_options );
	}
}
