<?php
/**
 * Plugin Name: CLOUDCUSP LLMS.txt Generator
 * Plugin URI: https://cloudcusp.com/llms-txt-generator-validator-1-click-fix-for-ai-seo/
 * Description: Generate AI-friendly LLMS.txt files for your WordPress site and allow or block any bot accessing it with modern UI/UX
 * Version: 1.0.5
 * Author: CLOUDCUSP
 * Author URI: https://cloudcusp.com
 * License: GPL v2 or later
 * Text Domain: cloudcusp-llms-txt-generator
 */

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

// Define plugin constants
define('CCSPLLMS_VERSION', '1.0.5');
define('CCSPLLMS_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('CCSPLLMS_PLUGIN_URL', plugin_dir_url(__FILE__));
define('CCSPLLMS_PLUGIN_BASENAME', plugin_basename(__FILE__));

// Activation hook
register_activation_hook(__FILE__, 'ccspllms_activate_plugin');
function ccspllms_activate_plugin() {
    require_once CCSPLLMS_PLUGIN_DIR . 'includes/class-ccspllms-database.php';
    $database = new CCSPLLMS_Database();
    $database->create_tables();
    
    // Set default options
    add_option('ccspllms_version', CCSPLLMS_VERSION);
    add_option('ccspllms_enabled', 1);
    add_option('ccspllms_post_types', array('post', 'page'));
    add_option('ccspllms_excerpt_priority', 'excerpt_meta_content');
    add_option('ccspllms_word_limit', 50);
    add_option('ccspllms_use_canonical', 1);
    
    // Schedule cron job
    if (!wp_next_scheduled('ccspllms_generate_file')) {
        wp_schedule_event(time(), 'daily', 'ccspllms_generate_file');
    }
}

// Deactivation hook
register_deactivation_hook(__FILE__, 'ccspllms_deactivate_plugin');
function ccspllms_deactivate_plugin() {
    wp_clear_scheduled_hook('ccspllms_generate_file');
}

// Load plugin classes
require_once CCSPLLMS_PLUGIN_DIR . 'includes/class-ccspllms-core.php';
require_once CCSPLLMS_PLUGIN_DIR . 'includes/class-ccspllms-generator.php';
require_once CCSPLLMS_PLUGIN_DIR . 'includes/class-ccspllms-analytics.php';
require_once CCSPLLMS_PLUGIN_DIR . 'includes/class-ccspllms-cron.php';
require_once CCSPLLMS_PLUGIN_DIR . 'includes/class-ccspllms-database.php';

if (is_admin()) {
    require_once CCSPLLMS_PLUGIN_DIR . 'admin/class-ccspllms-admin.php';
}

// Initialize the plugin
function ccspllms_init() {
    $ccspllms_core = new CCSPLLMS_Core();
    $ccspllms_core->init();
}
add_action('plugins_loaded', 'ccspllms_init');

// Handle llms.txt requests
add_action('init', 'ccspllms_handle_request');
function ccspllms_handle_request() {
    if (isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI'] === '/llms.txt') {
        $generator = new CCSPLLMS_Generator();
        $content = $generator->get_cached_content();
        
        if ($content) {
            // Log the request
            $analytics = new CCSPLLMS_Analytics();
            $analytics->log_request();
            
            header('Content-Type: text/plain; charset=utf-8');
            echo esc_html($content);
            exit;
        }
    }
}

// Add settings link to plugins page
add_filter('plugin_action_links_' . CCSPLLMS_PLUGIN_BASENAME, 'ccspllms_add_settings_link');
function ccspllms_add_settings_link($links) {
    $settings_link = '<a href="admin.php?page=ccspllms-generator">Settings</a>';
    array_unshift($links, $settings_link);
    return $links;
}