<?php

/**
 * Core plugin orchestrator.
 *
 * @package Copyfend
 */
defined( 'ABSPATH' ) || exit;
/**
 * Main plugin class. Loads dependencies and wires up the frontend + admin.
 */
final class CShield {
    /**
     * Singleton instance.
     *
     * @var CShield|null
     */
    private static $instance = null;

    /**
     * Settings handler.
     *
     * @var CShield_Settings
     */
    public $settings;

    /**
     * Frontend protection handler.
     *
     * @var CShield_Frontend
     */
    public $frontend;

    /**
     * Server-side anti-scraping handler.
     *
     * @var CShield_Server
     */
    public $server;

    /**
     * Attribution & copyright handler.
     *
     * @var CShield_Attribution
     */
    public $attribution;

    /**
     * Forensic watermark handler.
     *
     * @var CShield_Forensic
     */
    public $forensic;

    /**
     * Image watermark handler.
     *
     * @var CShield_Watermark
     */
    public $watermark;

    /**
     * Content locker handler.
     *
     * @var CShield_Locker
     */
    public $locker;

    /**
     * Honeypot handler.
     *
     * @var CShield_Honeypot
     */
    public $honeypot;

    /**
     * Admin settings screen.
     *
     * @var CShield_Admin|null
     */
    public $admin = null;

    /**
     * Boot (or return) the single instance.
     *
     * @return CShield
     */
    public static function instance() {
        if ( null === self::$instance ) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * Constructor.
     */
    private function __construct() {
        $this->load_textdomain();
        $this->includes();
        $this->init();
    }

    /**
     * Load translations.
     */
    private function load_textdomain() {
        add_action( 'init', static function () {
        } );
    }

    /**
     * Require class files.
     */
    private function includes() {
        // Free core.
        require_once CSHIELD_DIR . 'includes/class-cs-settings.php';
        require_once CSHIELD_DIR . 'includes/class-cs-ip.php';
        require_once CSHIELD_DIR . 'includes/class-cs-frontend.php';
        require_once CSHIELD_DIR . 'includes/class-cs-attribution.php';
        require_once CSHIELD_DIR . 'includes/class-cs-locker.php';
        // Pro modules — these files ship only in Copyfend Pro.
        foreach ( array(
            'class-cs-server.php',
            'class-cs-forensic.php',
            'class-cs-forensic-log.php',
            'class-cs-watermark.php',
            'class-cs-honeypot.php'
        ) as $pro_file ) {
            $path = CSHIELD_DIR . 'includes/' . $pro_file;
            if ( file_exists( $path ) ) {
                require_once $path;
            }
        }
        if ( is_admin() ) {
            $pro_admin = CSHIELD_DIR . 'admin/class-cs-admin-pro.php';
            if ( file_exists( $pro_admin ) ) {
                require_once $pro_admin;
            }
            require_once CSHIELD_DIR . 'admin/class-cs-admin.php';
        }
    }

    /**
     * Instantiate components.
     */
    private function init() {
        // Free features — always available.
        $this->settings = new CShield_Settings();
        $this->frontend = new CShield_Frontend($this->settings);
        $this->attribution = new CShield_Attribution($this->settings);
        $this->locker = new CShield_Locker($this->settings);
        CShield_Locker::maybe_install();
        // Pro modules — present only in Copyfend Pro, and only with a license.
        if ( self::is_pro() && class_exists( 'CShield_Server' ) ) {
            $this->server = new CShield_Server($this->settings);
            $this->forensic = new CShield_Forensic($this->settings);
            $this->watermark = new CShield_Watermark($this->settings);
            $this->honeypot = new CShield_Honeypot($this->settings);
            CShield_Forensic_Log::maybe_install();
        }
        if ( is_admin() ) {
            $this->admin = new CShield_Admin($this->settings);
        }
    }

    /**
     * Whether the active install can run premium (PRO) code.
     *
     * @return bool
     */
    public static function is_pro() {
        return function_exists( 'cshield_fs' ) && cshield_fs()->can_use_premium_code();
    }

    /**
     * Activation callback: seed default options.
     */
    public static function activate() {
        require_once CSHIELD_DIR . 'includes/class-cs-settings.php';
        require_once CSHIELD_DIR . 'includes/class-cs-locker.php';
        CShield_Settings::install_defaults();
        CShield_Locker::install();
        // The forensic log ships only in Copyfend Pro.
        $log = CSHIELD_DIR . 'includes/class-cs-forensic-log.php';
        if ( file_exists( $log ) ) {
            require_once $log;
            CShield_Forensic_Log::install();
        }
    }

    /**
     * Deactivation callback.
     */
    public static function deactivate() {
        // No persistent runtime state to tear down yet.
    }

    /**
     * Uninstall cleanup — run via the Freemius `after_uninstall` action.
     *
     * Note: watermark ".cs-original" image backups are intentionally left in
     * place — they may be the only un-watermarked copy of the user's images.
     */
    public static function uninstall() {
        global $wpdb;
        delete_option( 'cshield_settings' );
        delete_option( 'cshield_stats' );
        delete_option( 'cshield_copy_stats' );
        delete_option( 'cshield_onboarding' );
        delete_option( 'cshield_log_db' );
        delete_option( 'cshield_leads_db' );
        delete_option( 'cshield_blocklist' );
        delete_option( 'cshield_canary' );
        $tables = array($wpdb->prefix . 'cshield_log', $wpdb->prefix . 'cshield_leads');
        foreach ( $tables as $table ) {
            $wpdb->query( "DROP TABLE IF EXISTS {$table}" );
            // phpcs:ignore WordPress.DB.DirectDatabaseQuery,WordPress.DB.PreparedSQL.InterpolatedNotPrepared,PluginCheck.Security.DirectDB.UnescapedDBParameter -- Table name is built from $wpdb->prefix, not user input.
        }
    }

}
