<?php
/**
 * Sitemap REST API
 *
 * Exposes cached sitemap data as JSON endpoints.
 *
 * @package SEOvaultSEO
 */

namespace SEOvaultSEO\Sitemap;

use SEOvaultSEO\Core\Options;

/**
 * Headless JSON endpoints for sitemap data
 */
class Rest {

	/**
	 * REST namespace.
	 *
	 * @var string
	 */
	const NAMESPACE = 'seovaultai/v1';

	/**
	 * Initialize REST routes.
	 */
	public static function init() {
		add_action( 'rest_api_init', array( __CLASS__, 'register_routes' ) );
	}

	/**
	 * Register REST routes.
	 */
	public static function register_routes() {
		register_rest_route(
			self::NAMESPACE,
			'/sitemap',
			array(
				'methods'             => 'GET',
				'callback'            => array( __CLASS__, 'get_index' ),
				'permission_callback' => array( __CLASS__, 'permission_callback' ),
			)
		);

		register_rest_route(
			self::NAMESPACE,
			'/sitemap/(?P<slug>[a-z0-9_-]+)',
			array(
				'methods'             => 'GET',
				'callback'            => array( __CLASS__, 'get_sub_sitemap' ),
				'permission_callback' => array( __CLASS__, 'permission_callback' ),
				'args'                => array(
					'slug' => array(
						'required' => true,
						'type'     => 'string',
					),
				),
			)
		);
	}

	/**
	 * Permission callback for intentionally public sitemap endpoints.
	 *
	 * Sitemaps are public crawl data and require no authentication, matching
	 * WordPress core sitemap behavior. The enabled-module check remains the
	 * access gate so disabled sitemaps return a 404 response.
	 *
	 * @return bool|\WP_Error
	 */
	public static function permission_callback() {
		if ( ! Options::is_sitemap_enabled() ) {
			return new \WP_Error(
				'sitemap_disabled',
				__( 'Sitemap module is disabled.', 'seovault-ai' ),
				array( 'status' => 404 )
			);
		}
		return true;
	}

	/**
	 * Get the sitemap index as JSON.
	 *
	 * @return \WP_REST_Response|\WP_Error
	 */
	public static function get_index() {
		$index = Cache::read_index();
		if ( empty( $index ) ) {
			return new \WP_Error(
				'sitemap_not_found',
				__( 'Sitemap index not found.', 'seovault-ai' ),
				array( 'status' => 404 )
			);
		}

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

	/**
	 * Get a sub-sitemap as JSON.
	 *
	 * @param \WP_REST_Request $request Request.
	 * @return \WP_REST_Response|\WP_Error
	 */
	public static function get_sub_sitemap( $request ) {
		$slug = Sanitizer::slug( $request->get_param( 'slug' ) );
		if ( '' === $slug ) {
			return new \WP_Error(
				'invalid_sitemap',
				__( 'Invalid sitemap slug.', 'seovault-ai' ),
				array( 'status' => 404 )
			);
		}

		$data = Cache::get( $slug . '.json' );
		if ( false === $data ) {
			$data = Cache::get( 'sitemap/' . $slug . '.json' );
		}

		if ( false === $data ) {
			return new \WP_Error(
				'sitemap_not_found',
				__( 'Sitemap not found.', 'seovault-ai' ),
				array( 'status' => 404 )
			);
		}

		$decoded = json_decode( $data, true );
		if ( ! is_array( $decoded ) ) {
			return new \WP_Error(
				'invalid_sitemap',
				__( 'Invalid sitemap data.', 'seovault-ai' ),
				array( 'status' => 404 )
			);
		}

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