<?php
/**
 * Comment Security Layer Addon bootstrap.
 *
 * @package Polanger_Dashboard_Tools
 */

if ( ! defined( 'ABSPATH' ) ) {
  exit;
}

require_once __DIR__ . '/includes/class-comment-security-settings.php';
require_once __DIR__ . '/includes/class-comment-security-logger.php';
require_once __DIR__ . '/includes/class-comment-security-engine.php';
require_once __DIR__ . '/includes/class-comment-security-page.php';

/**
 * Always-on registrar so the addon shows up on the Addons page even
 * before activation. The full runtime is only initialized when the
 * addon is active (see plugin bootstrap).
 */
add_filter(
  'pdt_register_addons',
  /**
   * @param array $addons Addons map.
   * @return array
   */
  function ( $addons ) {
    if ( ! is_array( $addons ) ) {
      $addons = array();
    }
    if ( ! isset( $addons['comment-security'] ) ) {
      $addons['comment-security'] = array(
        'title'       => __( 'Comment Security Layer', 'polanger-admin-menu-manager' ),
        'description' => __( 'Layered comment protection: honeypot, timing, link/keyword rules, flood control, behavior score, and silent mode.', 'polanger-admin-menu-manager' ),
        'status'      => 'inactive',
        'slug'        => 'polanger-settings',
        'is_core'     => false,
      );
    }
    return $addons;
  }
);

add_action(
  'init',
  /**
   * Clear scheduled tasks when the addon is not active.
   *
   * @return void
   */
  function () {
    if ( ! function_exists( 'pdt_is_addon_active' ) || pdt_is_addon_active( 'comment-security' ) ) {
      return;
    }

    $timestamp = wp_next_scheduled( 'pdt_comment_security_daily_purge' );
    while ( $timestamp ) {
      wp_unschedule_event( $timestamp, 'pdt_comment_security_daily_purge' );
      $timestamp = wp_next_scheduled( 'pdt_comment_security_daily_purge' );
    }
  },
  20
);

/**
 * Main addon orchestrator for the Comment Security Layer.
 */
class PDT_Comment_Security {

  /**
   * Settings service.
   *
   * @var PDT_Comment_Security_Settings
   */
  private $settings;

  /**
   * Logger service.
   *
   * @var PDT_Comment_Security_Logger
   */
  private $logger;

  /**
   * Protection engine.
   *
   * @var PDT_Comment_Security_Engine
   */
  private $engine;

  /**
   * Settings page renderer.
   *
   * @var PDT_Comment_Security_Page
   */
  private $page;

  /**
   * Constructor.
   */
  public function __construct() {
    $this->settings = new PDT_Comment_Security_Settings();
    $this->logger   = new PDT_Comment_Security_Logger();
    $this->engine   = new PDT_Comment_Security_Engine( $this->settings, $this->logger );
    $this->page     = new PDT_Comment_Security_Page( $this->settings, $this->logger );

    // Engine wires WordPress comment hooks internally.
    $this->engine->register_hooks();

    // Register the "Comments" tab on the core Settings page.
    add_filter( 'pdt_settings_tabs', array( $this->page, 'register_tab' ) );
    add_filter( 'pdt_settings_tab_icons', array( $this->page, 'register_tab_icon' ) );
    add_action( 'pdt_settings_render_tab_comments', array( $this->page, 'render_tab' ) );

    // Process settings save handler before tab is rendered.
    add_action( 'admin_init', array( $this->page, 'maybe_handle_save' ) );

    // Enqueue our settings tab assets only on the Settings page.
    add_action( 'admin_enqueue_scripts', array( $this->page, 'enqueue_assets' ) );

    // Lightweight ensure-table maintenance for admin and frontend requests.
    add_action( 'init', array( $this->logger, 'maybe_install_table' ), 5 );
  }
}
