<?php

/**
 * Plugin Name:       Cine Movie Engine
 * Plugin URI:        https://movieengine.pro
 * Description:       Build an OTT streaming platform in WordPress with movies, TV series, TMDb import, and secure HLS video playback.
 * Version:           1.0.1
 * Author:            ThemePul
 * Text Domain:       cine-movie-engine
 * Domain Path:       /languages
 * Requires at least: 5.8
 * Requires PHP:      7.4
 * License:           GPL v2 or later
 */

// If this file is called directly, abort.
if (! defined("ABSPATH")) {
    exit; // Exit if accessed directly
}

// Define Constants
define('MOVIE_ENGINE_VERSION', '1.0.1');
define('MOVIE_ENGINE_PATH', plugin_dir_path(__FILE__));
define('MOVIE_ENGINE_URL', plugin_dir_url(__FILE__));
define('MOVIE_ENGINE_BASENAME', plugin_basename(__FILE__));

// Gutenberg blocks (self-contained; does not modify shortcode classes).
require_once MOVIE_ENGINE_PATH . 'gutenberg/bootstrap.php';

// Check if Pro version is active.
$pro_plugin_path = 'cine-movie-engine-pro/cine-movie-engine-pro.php';
$is_pro_active = false;

if (in_array($pro_plugin_path, (array) get_option('active_plugins', array()))) {
    $is_pro_active = true;
}

if (is_multisite()) {
    $network_active_plugins = get_site_option('active_sitewide_plugins', array());
    if (isset($network_active_plugins[$pro_plugin_path])) {
        $is_pro_active = true;
    }
}

if (function_exists('movieengine_fs')) {
    movieengine_fs()->set_basename(false, __FILE__);
} else {
    if ($is_pro_active) {

        add_action('plugins_loaded', function () {
            if (function_exists('movieengine_fs')) {
                movieengine_fs()->set_basename(false, __FILE__);
            }
        });
    } else {
        if (! function_exists('movieengine_fs')) {
            function movieengine_fs() {
                global $movieengine_fs;

                if (! isset($movieengine_fs)) {
                    if (! defined('WP_FS__PRODUCT_24279_MULTISITE')) {
                        define('WP_FS__PRODUCT_24279_MULTISITE', true);
                    }
                    require_once dirname(__FILE__) . '/freemius/start.php';

                    $movieengine_fs = fs_dynamic_init(array(
                        'id'                  => '24279',
                        'slug'                => 'cine-movie-engine',
                        'type'                => 'plugin',
                        'public_key'          => 'pk_e224f9fbb71a3361b44f930fd3c57',
                        'is_premium'          => false,
                        'is_premium_only'     => false,
                        'premium_suffix'      => 'Pro',
                        'is_org_compliant'    => true,
                        'has_premium_version' => true,
                        'has_addons'          => false,
                        'has_paid_plans'      => true,
                        'menu'                => array(
                            'slug'           => 'cine-movie-engine',
                            'first-path'     => 'admin.php?page=cine-movie-engine',
                            'network'        => true,
                            'contact'    => false,
                            'support'    => false,
                            'pricing'    => false,
                            'account'    => false,
                        ),
                    ));
                }

                return $movieengine_fs;
            }

            // Init Freemius.
            movieengine_fs();
            // Signal that SDK was initiated.
            do_action('movieengine_fs_loaded');

            movieengine_fs()->add_filter('deactivate_on_activation', '__return_false');
            movieengine_fs()->add_filter('hide_freemius_powered_by', '__return_true');
        }
    }
}

// Autoloader
spl_autoload_register(function ($class) {
    $prefix = 'MovieEngine\\';
    $base_dir = MOVIE_ENGINE_PATH . 'includes/';

    $len = strlen($prefix);
    if (strncmp($prefix, $class, $len) !== 0) {
        return;
    }

    $relative_class = substr($class, $len);
    $file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';

    if (file_exists($file)) {
        require $file;
    }
});

// Initialize the Plugin
function movie_engine_init() {
    $plugin = \MovieEngine\Core\Plugin::get_instance();

    // Initialize cron management
    \MovieEngine\Core\CronManager::init();

    // Schedule cleanup cron event
    \MovieEngine\Core\CronManager::schedule_cleanup();
}
add_action('plugins_loaded', 'movie_engine_init');

// Activation/Deactivation Hooks
if (function_exists('register_activation_hook')) {
    register_activation_hook(__FILE__, [\MovieEngine\Core\Activator::class, 'activate']);
}

if (function_exists('register_deactivation_hook')) {
    register_deactivation_hook(__FILE__, [\MovieEngine\Core\Deactivator::class, 'deactivate']);
}
