<?php
if (!defined('ABSPATH')) {
	exit;
}

class ProbonoSEO_Robots_AIO {
	private static $instance = null;

	private static $crawlers = array(
		'GPTBot',
		'ClaudeBot',
		'PerplexityBot',
		'Googlebot-Extended',
		'Meta-ExternalAgent',
		'Applebot-Extended',
		'Amazonbot',
		'YouBot',
		'cohere-ai',
		'anthropic-ai',
	);

	public static function get_instance() {
		if (self::$instance === null) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	private function __construct() {
		$this->init_hooks();
	}

	private function init_hooks() {
		if (get_option('probonoseo_aio_robots', '1') === '1') {
			add_filter('robots_txt', array($this, 'append_ai_crawlers'), 10, 2);
		}
	}

	public function append_ai_crawlers($output, $public) {
		if ($public === '0') {
			return $output;
		}

		$lines = array();

		foreach (self::$crawlers as $crawler) {
			$allow_line = 'User-agent: ' . $crawler . "\nAllow: /";
			if (strpos($output, 'User-agent: ' . $crawler) === false) {
				$lines[] = $allow_line;
			}
		}

		if (!empty($lines)) {
			$output .= "\n" . implode("\n\n", $lines) . "\n";
		}

		return $output;
	}
}

function probonoseo_init_robots_aio() {
	ProbonoSEO_Robots_AIO::get_instance();
}
add_action('init', 'probonoseo_init_robots_aio');