<?php

defined( 'ABSPATH' ) || exit;
require_once PageVICO_PLUGIN_DIR . 'includes/autoloader.php';
use PageVICO\Admin\AdminPage;
use PageVICO\Sessions\SessionManager;
use PageVICO\Tracking\TrackerInit;
use PageVICO\API\RouteRegistrar;
use PageVICO\Admin\SettingsPage;
use PageVICO\Admin\AnalyticsPages;
use PageVICO\Cleanup\DataCleanup;
use PageVICO\Database\Installer;
AdminPage::init();
SessionManager::init();
TrackerInit::init();
RouteRegistrar::init();
SettingsPage::init();
AnalyticsPages::init();
DataCleanup::init();
// Register activation hook to set up database tables and default settings
register_activation_hook( PageVICO_PLUGIN_FILE, function () {
    require_once PageVICO_PLUGIN_DIR . 'includes/Database/Installer.php';
    require_once PageVICO_PLUGIN_DIR . 'includes/Settings/SettingsManager.php';
    PageVICO\Database\Installer::install();
    PageVICO\Settings\SettingsManager::initializeSettings();
} );
// Register the cron event if it doesn't exist
add_action( 'init', function () {
    if ( !wp_next_scheduled( 'pagevico_expire_sessions' ) ) {
        // Schedule the event to run every 15 minutes
        wp_schedule_event( time(), 'fifteen_minutes', 'pagevico_expire_sessions' );
    }
    // Schedule GeoIP DB refresh every 30 days
    if ( !wp_next_scheduled( 'pagevico_refresh_geo_db' ) ) {
        wp_schedule_event( time(), 'daily', 'pagevico_refresh_geo_db' );
        // use daily for now
    }
    // Check and create pro tables if needed
    // Only check and create pro tables on plugin activation or when explicitly needed
    if ( pvca_fs()->is__premium_only() && get_option( 'pagevico_pro_tables_created' ) !== 'yes' ) {
        require_once PageVICO_PLUGIN_DIR . 'includes/Database/Installer.php';
        $charset_collate = $GLOBALS['wpdb']->get_charset_collate();
        $mysql_version = $GLOBALS['wpdb']->get_var( "SELECT VERSION()" );
        $partition_support = version_compare( $mysql_version, '5.7.0', '>=' );
        PageVICO\Database\Installer::create_pro_tables__premium_only( $charset_collate, $partition_support );
        update_option( 'pagevico_pro_tables_created', 'yes' );
    }
} );
// Register the session expiration event
add_action( 'pagevico_expire_sessions', function () {
    if ( class_exists( 'PageVICO\\Sessions\\SessionManager' ) ) {
        PageVICO\Sessions\SessionManager::expireInactiveSessions();
    }
} );
// Define the fifteen_minutes interval if it doesn't exist
add_filter( 'cron_schedules', function ( $schedules ) {
    if ( !isset( $schedules['fifteen_minutes'] ) ) {
        $schedules['fifteen_minutes'] = array(
            'interval' => 15 * 60,
            'display'  => __( 'Every 15 minutes' ),
        );
    }
    return $schedules;
} );
// Register database partition management hook
add_action( 'pagevico_manage_partitions', array('PageVICO\\Database\\Installer', 'manage_partitions') );
// Trigger first download
if ( class_exists( 'PageVICO\\Geo\\GeoService' ) ) {
    PageVICO\Geo\GeoService::ensureDatabase();
}
add_action( 'pagevico_refresh_geo_db', array('PageVICO\\Geo\\GeoService', 'downloadDatabase') );
// Add cache clearing hooks
add_action( 'pagevico_after_pageview_recorded', array('PageVICO\\Cache\\CacheManager', 'clearOnNewPageview') );
add_action( 'pagevico_after_session_created', array('PageVICO\\Cache\\CacheManager', 'clearOnNewSession') );
// Initialize AJAX handlers
add_action( 'init', array('PageVICO\\Ajax\\CacheHandler', 'init') );
function pagevico_load_textdomain() {
    load_plugin_textdomain( 'page-visit-counter-analytics', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}

add_action( 'plugins_loaded', 'pagevico_load_textdomain' );