<?php
/**
 * AI Visibility - Robots.txt Management
 *
 * @package SEOvaultSEO
 */

namespace SEOvaultSEO\AiVisibility;

use SEOvaultSEO\Utils\Logger;

/**
 * Handles robots.txt management
 */
class Robots {

	/**
	 * Robots block delimiters
	 */
	const BEGIN_MARKER = '# BEGIN SEOVault AI Crawler Policy';
	const END_MARKER   = '# END SEOVault AI Crawler Policy';

	/**
	 * Initialize robots.txt management
	 */
	public static function init() {
		if ( ! \SEOvaultSEO\Compatibility\Detector::should_output_seo() ) {
			return;
		}

		add_filter( 'robots_txt', array( __CLASS__, 'modify_robots_txt' ), 10, 2 );
	}

	/**
	 * Modify robots.txt
	 *
	 * @param string $output Robots.txt content
	 * @param bool   $public Whether the site is public
	 * @return string
	 */
	public static function modify_robots_txt( $output, $public ) {
		if ( ! $public ) {
			return $output;
		}

		$policy = \SEOvaultSEO\Core\Options::get_robots_policy();

		if ( empty( $policy ) ) {
			return $output;
		}

		$policy_content = self::get_policy_content( $policy );

		if ( empty( $policy_content ) ) {
			return $output;
		}

		// Remove existing SEOvault block if present
		$output = self::remove_existing_block( $output );

		// Append new block
		$output .= "\n" . self::BEGIN_MARKER . "\n";
		$output .= $policy_content;
		$output .= "\n" . self::END_MARKER . "\n";

		return $output;
	}

	/**
	 * Get policy content for a given policy
	 *
	 * @param string $policy Policy name
	 * @return string
	 */
	private static function get_policy_content( $policy ) {
		switch ( $policy ) {
			case 'balanced':
				return self::get_balanced_policy();
			case 'maximum_visibility':
				return self::get_maximum_visibility_policy();
			case 'strict':
				return self::get_strict_policy();
			case 'custom':
				return \SEOvaultSEO\Core\Options::get_robots_custom_content();
			default:
				return '';
		}
	}

	/**
	 * Get balanced policy
	 *
	 * @return string
	 */
	private static function get_balanced_policy() {
		return "User-agent: *\nAllow: /\nCrawl-delay: 1";
	}

	/**
	 * Get maximum visibility policy
	 *
	 * @return string
	 */
	private static function get_maximum_visibility_policy() {
		return "User-agent: *\nAllow: /\nCrawl-delay: 0";
	}

	/**
	 * Get strict policy
	 *
	 * @return string
	 */
	private static function get_strict_policy() {
		return "User-agent: *\nAllow: /\nCrawl-delay: 5";
	}

	/**
	 * Remove existing SEOvault block from robots.txt
	 *
	 * @param string $output Robots.txt content
	 * @return string
	 */
	private static function remove_existing_block( $output ) {
		$pattern = '/' . preg_quote( self::BEGIN_MARKER, '/' ) . '.*?' . preg_quote( self::END_MARKER, '/' ) . '/s';
		return preg_replace( $pattern, '', $output );
	}

	/**
	 * Get robots status for API
	 *
	 * @return array
	 */
	public static function get_status() {
		$policy = \SEOvaultSEO\Core\Options::get_robots_policy();

		return array(
			'policy'     => $policy,
			'has_policy' => ! empty( $policy ),
		);
	}

	/**
	 * Preview robots policy
	 *
	 * @param string $policy         Policy name.
	 * @param string $custom_content Custom robots.txt content (used when policy is 'custom').
	 * @return array
	 */
	public static function preview( $policy, $custom_content = '' ) {
		$policy         = \SEOvaultSEO\Utils\Sanitizer::sanitize_robots_policy( $policy );
		$custom_content = \SEOvaultSEO\Utils\Sanitizer::sanitize_robots_custom_content( $custom_content );

		if ( 'custom' === $policy ) {
			$content = '' !== $custom_content ? $custom_content : \SEOvaultSEO\Core\Options::get_robots_custom_content();
		} else {
			$content = self::get_policy_content( $policy );
		}

		return array(
			'success' => true,
			'preview' => $content,
		);
	}

	/**
	 * Apply robots policy
	 *
	 * @param string $policy Policy name
	 * @return array
	 */
	public static function apply( $policy, $custom_content = '' ) {
		$policy         = \SEOvaultSEO\Utils\Sanitizer::sanitize_robots_policy( $policy );
		$custom_content = \SEOvaultSEO\Utils\Sanitizer::sanitize_robots_custom_content( $custom_content );

		Logger::log( 'Robots::apply: entered', array( 'policy' => $policy ) );

		\SEOvaultSEO\Core\Options::set_robots_policy( $policy );

		if ( 'custom' === $policy ) {
			\SEOvaultSEO\Core\Options::set_robots_custom_content( $custom_content );
		}

		Logger::log( 'Robots::apply: success', array( 'policy' => $policy ) );

		return array(
			'success' => true,
			'message' => 'Robots policy applied',
		);
	}

	/**
	 * Restore default robots.txt
	 *
	 * @return array
	 */
	public static function restore() {
		Logger::log( 'Robots::restore: entered' );
		\SEOvaultSEO\Core\Options::set_robots_policy( '' );
		\SEOvaultSEO\Core\Options::set_robots_custom_content( '' );
		Logger::log( 'Robots::restore: success' );

		return array(
			'success' => true,
			'message' => 'Robots policy restored to default',
		);
	}
}
