<?php
/**
 * Plugin Name: Tiriri Transparency for EU AI Act
 * Description: Detect AI tools on your WordPress site and implement EU AI Act Article 50 transparency disclosures. Auto-scan for AI chatbots, label AI-generated content, and generate compliance reports.
 * Version:     1.0.0
 * Author:      wpplugins2026
 * Author URI:  https://profiles.wordpress.org/wpplugins2026/
 * License:     GPL v2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: tiriri-transparency-for-eu-ai-act
 * Domain Path: /languages
 * Requires at least: 6.0
 * Requires PHP: 7.4
 */

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

// Plugin constants
define( 'TIRIRI_VERSION', '1.0.0' );
define( 'TIRIRI_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'TIRIRI_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'TIRIRI_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );

/**
 * Initialize Freemius SDK
 */
function tiriri_fs() {
    global $tiriri_fs;

    if ( ! isset( $tiriri_fs ) ) {
        // Only load the bundled Freemius SDK if another plugin hasn't already loaded one.
        if ( ! function_exists( 'fs_dynamic_init' ) ) {
            require_once TIRIRI_PLUGIN_DIR . 'freemius/start.php';
        }

        $tiriri_fs = fs_dynamic_init( array(
            'id'                  => '27371',
            'slug'                => 'tiriri-transparency-for-eu-ai-act',
            'type'                => 'plugin',
            'public_key'          => 'pk_dc4f45b0a95935c40676406280d6c',
            'is_premium'          => false,
            'has_addons'          => false,
            'has_paid_plans'      => true,
            'menu'                => array(
                'slug'    => 'tiriri-transparency-for-eu-ai-act',
                'support' => false,
            ),
        ) );
    }

    return $tiriri_fs;
}

// Init Freemius
tiriri_fs();
// Signal that SDK was initiated
do_action( 'tiriri_fs_loaded' );

/**
 * Helper: Check if current install is Pro
 */
function tiriri_is_pro() {
    return tiriri_fs()->is_paying();
}

/**
 * Main plugin class
 */
final class Tiriri_Plugin {

    /**
     * Single instance
     */
    private static $instance = null;

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

    /**
     * Constructor
     */
    private function __construct() {
        $this->load_dependencies();
        $this->init_hooks();
    }

    /**
     * Load required files
     */
    private function load_dependencies() {
        require_once TIRIRI_PLUGIN_DIR . 'includes/class-scanner.php';
        require_once TIRIRI_PLUGIN_DIR . 'includes/class-disclosure.php';
        require_once TIRIRI_PLUGIN_DIR . 'includes/class-content-labeler.php';
        require_once TIRIRI_PLUGIN_DIR . 'includes/class-settings.php';

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

    /**
     * Initialize hooks
     */
    private function init_hooks() {
        // Activation / deactivation
        register_activation_hook( __FILE__, array( $this, 'activate' ) );
        register_deactivation_hook( __FILE__, array( $this, 'deactivate' ) );

        // Init
        add_action( 'init', array( $this, 'load_textdomain' ) );

        // Privacy policy
        add_action( 'admin_init', array( $this, 'add_privacy_policy_content' ) );

        // Initialize components
        add_action( 'plugins_loaded', array( $this, 'init_components' ) );
    }

    /**
     * Plugin activation
     */
    public function activate() {
        // Set default options
        $defaults = array(
            'chatbot_disclosure_enabled'  => true,
            'chatbot_disclosure_text'     => __( 'You are interacting with an AI assistant.', 'tiriri-transparency-for-eu-ai-act' ),
            'chatbot_disclosure_position' => 'before_chat',
            'chatbot_disclosure_style'    => 'banner',
            'content_labeling_enabled'    => false,
            'scan_third_party_scripts'    => true,
            'disclosure_language'         => 'auto',
        );

        if ( false === get_option( 'tiriri_settings' ) ) {
            add_option( 'tiriri_settings', $defaults );
        }

        // Store activation time for deadline countdown
        if ( false === get_option( 'tiriri_installed' ) ) {
            add_option( 'tiriri_installed', time() );
        }

        // Run initial scan
        $scanner = new Tiriri_Scanner();
        $scanner->run_scan();

        // Flush rewrite rules
        flush_rewrite_rules();
    }

    /**
     * Plugin deactivation
     */
    public function deactivate() {
        // Clean up scheduled events
        wp_clear_scheduled_hook( 'tiriri_daily_scan' );
        flush_rewrite_rules();
    }

    /**
     * Load translations
     */
    public function load_textdomain() {
        load_plugin_textdomain(
            'tiriri-transparency-for-eu-ai-act',
            false,
            dirname( TIRIRI_PLUGIN_BASENAME ) . '/languages'
        );
    }

    /**
     * Add privacy policy suggested text
     */
    public function add_privacy_policy_content() {
        if ( ! function_exists( 'wp_add_privacy_policy_content' ) ) {
            return;
        }

        $content = sprintf(
            '<h2>%s</h2><p>%s</p><p>%s</p>',
            __( 'Tiriri', 'tiriri-transparency-for-eu-ai-act' ),
            __( 'This plugin scans for AI-powered tools on this website and stores the scan results in the WordPress database. It also stores AI content status labels (e.g., "AI-generated", "AI-assisted") as metadata on individual posts and pages. No personal visitor data is collected by the scanning or labeling features.', 'tiriri-transparency-for-eu-ai-act' ),
            __( 'A dismissible disclosure banner may set a cookie (tiriri_dismissed) in the visitor\'s browser to remember the dismissal for 24 hours. This cookie contains no personal data.', 'tiriri-transparency-for-eu-ai-act' )
        );

        wp_add_privacy_policy_content( 'Tiriri', wp_kses_post( $content ) );
    }

    /**
     * Initialize plugin components
     */
    public function init_components() {
        // Scanner - runs daily to detect new AI tools
        $scanner = new Tiriri_Scanner();
        $scanner->init();

        // Disclosure - handles frontend disclosure banners
        $disclosure = new Tiriri_Disclosure();
        $disclosure->init();

        // Content Labeler - adds AI content labels to posts/pages
        $labeler = new Tiriri_Content_Labeler();
        $labeler->init();

        // Settings
        $settings = new Tiriri_Settings();
        $settings->init();

        // Admin
        if ( is_admin() ) {
            $admin = new Tiriri_Admin();
            $admin->init();
        }
    }
}

// Initialize the plugin
Tiriri_Plugin::get_instance();
