<?php

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

class AIEO_Retention_Bootstrap {

	private static $loaded = false;

	public static function init() {
		if ( self::$loaded ) { return; }
		self::$loaded = true;

		self::require_dependencies();

		
		add_action( 'plugins_loaded', array( __CLASS__, 'install_schemas' ), 20 );

		
		add_action( 'init', array( __CLASS__, 'wire_crons' ), 11 );

		
		add_action( 'aieo_archive_run', array( 'AIEO_Archive_Handlers', 'handle' ), 10, 1 );
	}

	private static function require_dependencies() {
		$base = plugin_dir_path( dirname( __FILE__ ) );
		require_once $base . 'includes/sql/sql-aggregate-rollups.php';
		require_once $base . 'includes/sql/sql-remote-export-state.php';
		require_once $base . 'includes/class-aieo-remote-export-manager.php';
		require_once $base . 'includes/cron/class-aieo-aggregate-cron.php';
		require_once $base . 'includes/cron/class-aieo-archive-handlers.php';
		require_once $base . 'includes/cron/class-aieo-remote-export-cron.php';
		require_once $base . 'includes/cron/class-aieo-attribution-backfill-cron.php';
	}

	public static function install_schemas() {
		if ( function_exists( 'maybe_install_rollup_schema' ) ) {
			maybe_install_rollup_schema();
		}
		if ( function_exists( 'maybe_install_remote_export_schema' ) ) {
			maybe_install_remote_export_schema();
		}
	}

	public static function wire_crons() {
		
		if ( class_exists( 'AIEO_Aggregate_Cron' ) ) {
			AIEO_Aggregate_Cron::maybe_schedule();
			add_action( AIEO_Aggregate_Cron::HOOK, array( 'AIEO_Aggregate_Cron', 'run' ) );
		}
		
		if ( class_exists( 'AIEO_Remote_Export_Cron' ) ) {
			AIEO_Remote_Export_Cron::register();
		}
		
		
		if ( class_exists( 'AIEO_Attribution_Backfill_Cron' ) ) {
			AIEO_Attribution_Backfill_Cron::maybe_schedule();
			add_action( AIEO_Attribution_Backfill_Cron::HOOK, array( 'AIEO_Attribution_Backfill_Cron', 'run' ) );
		}
	}
}
