<?php

namespace MovieEngine\Core;

if (! defined("ABSPATH")) {
    exit; // Exit if accessed directly
}

class Plugin {

    private static $instance = null;
    private $loader;

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

    private function __construct() {
        if (!get_option('movie_engine_db_update_fix_metadata_2026_02_11')) {
            \MovieEngine\Core\Database::create_tables();
            update_option('movie_engine_db_update_fix_metadata_2026_02_11', true);
        }
        $this->load_dependencies();
    }

    private function load_dependencies() {
        $this->loader = new Loader();

        // Load Cache Manager
        \MovieEngine\Core\Cache::init();

        // Load Core Classes
        new \MovieEngine\PostTypes\Movie();
        new \MovieEngine\PostTypes\TVSeries();
        new \MovieEngine\PostTypes\Season();
        new \MovieEngine\PostTypes\Episode();
        new \MovieEngine\PostTypes\Person();
        new \MovieEngine\PostTypes\Taxonomies();

        new \MovieEngine\Admin\Admin();
        new \MovieEngine\Admin\Settings();
        new \MovieEngine\Admin\MetaBoxes();
        new \MovieEngine\Admin\CustomColumns();
        new \MovieEngine\Admin\TmdbImport();
        new \MovieEngine\Admin\Dashboard();
        new \MovieEngine\Admin\SetupWizard();

        new \MovieEngine\Utils\TemplateLoader();
        new \MovieEngine\Utils\PageTemplates();
        new \MovieEngine\Utils\Seo();
        new \MovieEngine\Frontend\Query();
        new \MovieEngine\Frontend\Ajax();
        new \MovieEngine\Frontend\Assets();
        new \MovieEngine\Frontend\Redirects();
        new \MovieEngine\Frontend\Player();
        new \MovieEngine\Frontend\Shortcodes();
        new \MovieEngine\Frontend\Users\UserLibrary();
        new \MovieEngine\Frontend\Users\WatchHistory();
        new \MovieEngine\Frontend\Users\UserDashboard();
        new \MovieEngine\Frontend\Users\UserInteractions();
        new \MovieEngine\Frontend\Users\UserAccess();
        new \MovieEngine\Frontend\Users\ProfileManager();
        new \MovieEngine\Frontend\Users\Auth();
        new \MovieEngine\Frontend\Users\Devices();
        new \MovieEngine\Frontend\Users\Notifications();
        new \MovieEngine\Frontend\Design();
        \MovieEngine\Frontend\TemplateHooks::init(); // Render hooks for templates
        new \MovieEngine\Frontend\Reviews();
        new \MovieEngine\Frontend\ReviewRouter();
        new \MovieEngine\Api\TmdbClient();
        new \MovieEngine\Api\OmdbClient();
        new \MovieEngine\Monetization\AdManager();
        new \MovieEngine\Monetization\Affiliate();
        new \MovieEngine\Payments\Membership();
        new \MovieEngine\Emails\EmailManager();
        new \MovieEngine\Widgets\Widgets();

        // Register API Routes
        add_action('rest_api_init', function () {
            $stream_controller = new \MovieEngine\Api\Stream_Controller();
            $stream_controller->register_routes();
        });
    }


    public function get_loader() {
        return $this->loader;
    }
}
