<?php
/**
 * Plugin Name: FixLinks AI
 * Plugin URI:  https://fixlinks.getinfotoyou.com
 * Description: Finds broken links on your WordPress site and uses AI to suggest and apply 301 redirects automatically.
 * Version:     3.3.0
 * Author:      FixLinks AI
 * License:     GPL-2.0-or-later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: fixlinks-ai
 * Requires at least: 5.8
 * Requires PHP: 8.0
 */

defined('ABSPATH') || exit;

define('FIXLINKS_VERSION',  '3.3.0');
define('FIXLINKS_PATH',     plugin_dir_path(__FILE__));
define('FIXLINKS_URL',      plugin_dir_url(__FILE__));
define('FIXLINKS_API_BASE', 'https://fixlinks.getinfotoyou.com');

foreach (['class-db', 'class-license', 'class-crawler', 'class-ai',
          'class-redirects', 'class-cron', 'class-email', 'class-report'] as $c) {
    require_once FIXLINKS_PATH . "includes/{$c}.php";
}

register_activation_hook(__FILE__,   ['FixLinks_DB',      'create_table']);
register_activation_hook(__FILE__,   ['FixLinks_Cron',    'schedule']);
register_deactivation_hook(__FILE__,  ['FixLinks_Cron',    'unschedule']);
register_deactivation_hook(__FILE__,  ['FixLinks_License', 'deactivate_domain']);

add_filter('cron_schedules', ['FixLinks_Cron', 'add_custom_intervals']);

add_action('plugins_loaded', function () {
    load_plugin_textdomain('fixlinks-ai', false, dirname(plugin_basename(__FILE__)) . '/languages');
});

// Serve DB-based redirects on every page load — works on Nginx + Apache
add_action('template_redirect', ['FixLinks_Redirects', 'serve_redirect'], 1);

add_action('admin_menu', function () {
    add_menu_page(
        __('FixLinks AI', 'fixlinks-ai'),
        __('FixLinks AI', 'fixlinks-ai'),
        'manage_options',
        'fixlinks-dashboard',
        fn() => require FIXLINKS_PATH . 'admin/dashboard.php',
        'dashicons-admin-links',
        30
    );
    add_submenu_page(
        'fixlinks-dashboard',
        __('Settings', 'fixlinks-ai'),
        __('Settings', 'fixlinks-ai'),
        'manage_options',
        'fixlinks-settings',
        fn() => require FIXLINKS_PATH . 'admin/settings.php'
    );
});

add_action('admin_enqueue_scripts', function ($hook) {
    if (!str_contains($hook, 'fixlinks')) return;
    wp_enqueue_script('fixlinks-admin', FIXLINKS_URL . 'admin/admin.js', ['jquery'], FIXLINKS_VERSION, true);
    wp_enqueue_style('fixlinks-admin',  FIXLINKS_URL . 'admin/admin.css', [], FIXLINKS_VERSION);
    wp_localize_script('fixlinks-admin', 'fixlinksData', [
        'ajax_url' => admin_url('admin-ajax.php'),
        'nonce'    => wp_create_nonce('fixlinks_nonce'),
        'plan'     => get_option('fixlinks_license_plan', 'free'),
    ]);
});

add_action('admin_notices', function () {
    $mem = wp_convert_hr_to_bytes(ini_get('memory_limit'));
    if ($mem > 0 && $mem < 64 * MB_IN_BYTES) {
        echo '<div class="notice notice-warning is-dismissible"><p>'
           . esc_html__('FixLinks AI: PHP memory limit is below 64MB. Large site crawls may fail. Add define("WP_MEMORY_LIMIT","256M") to wp-config.php.', 'fixlinks-ai')
           . '</p></div>';
    }
});

// AJAX handlers
add_action('wp_ajax_fixlinks_run_crawl',         ['FixLinks_Crawler',   'ajax_run']);
add_action('wp_ajax_fixlinks_crawl_progress',    ['FixLinks_Crawler',   'ajax_progress']);
add_action('wp_ajax_fixlinks_get_suggestion',    ['FixLinks_AI',        'ajax_get_suggestion']);
add_action('wp_ajax_fixlinks_apply_redirect',    ['FixLinks_Redirects', 'ajax_apply']);
add_action('wp_ajax_fixlinks_rollback_redirect', ['FixLinks_Redirects', 'ajax_rollback']);
add_action('wp_ajax_fixlinks_save_settings',     ['FixLinks_License',   'ajax_save_settings']);

// Admin POST (file download — not AJAX)
add_action('admin_post_fixlinks_export_report',  ['FixLinks_Report',    'export']);

// Cron
add_action('fixlinks_crawl_event',     ['FixLinks_Crawler',   'run_scheduled']);
add_action('fixlinks_crawl_batch',     ['FixLinks_Crawler',   'process_batch_cron']);
add_action('fixlinks_weekly_email',    ['FixLinks_Email',     'send_digest']);
add_action('fixlinks_license_recheck', ['FixLinks_License',   'recheck']);
add_action('fixlinks_flush_hits',      ['FixLinks_Redirects', 'flush_hit_counts']);