<?php

/**
 * Main plugin bootstrap.
 *
 * @package WPDudeProducts
 */
declare (strict_types = 1);
namespace WPDude\Products;

use WPDude\Products\Admin\Menu;
use WPDude\Products\Admin\More_Plugins_Page;
use WPDude\Products\Admin\Orders_Page;
use WPDude\Products\Admin\Packing_Slip_Page;
use WPDude\Products\Admin\Picking_Page;
use WPDude\Products\Admin\Product_Meta_Box;
use WPDude\Products\Admin\Settings_Page;
use WPDude\Products\Admin\Shipping_Label_Page;
use WPDude\Products\Blocks\Buy_Block;
use WPDude\Products\Blocks\Images_Block;
use WPDude\Products\Blocks\Price_Block;
use WPDude\Products\Blocks\Product_Block;
use WPDude\Products\Database\Migrator;
use WPDude\Products\Emails\Email_Listener;
use WPDude\Products\Frontend\Assets;
use WPDude\Products\Frontend\Order_View;
use WPDude\Products\Frontend\Return_Pages;
use WPDude\Products\Frontend\Shortcodes;
use WPDude\Products\Licensing\Premium_Bootstrap;
use WPDude\Products\Orders\Post_Type as Order_Post_Type;
use WPDude\Products\Products\Post_Type as Product_Post_Type;
use WPDude\Products\Products\Taxonomy;
use WPDude\Products\REST\REST_Loader;
use WPDude\Products\Settings\Settings;
/**
 * Plugin container and lifecycle.
 */
final class Plugin {
    /**
     * Singleton instance.
     *
     * @var self|null
     */
    private static ?self $instance = null;

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

    /**
     * Activation routine.
     */
    public static function activate() : void {
        if ( version_compare( PHP_VERSION, '8.1', '<' ) ) {
            deactivate_plugins( WPDUDE_DC_BASENAME );
            wp_die( esc_html__( 'Dude Commerce requires PHP 8.1 or later.', 'dude-commerce' ), esc_html__( 'Plugin Activation Error', 'dude-commerce' ), array(
                'back_link' => true,
            ) );
        }
        global $wp_version;
        if ( version_compare( (string) $wp_version, '6.5', '<' ) ) {
            deactivate_plugins( WPDUDE_DC_BASENAME );
            wp_die( esc_html__( 'Dude Commerce requires WordPress 6.5 or later.', 'dude-commerce' ), esc_html__( 'Plugin Activation Error', 'dude-commerce' ), array(
                'back_link' => true,
            ) );
        }
        Migrator::install();
        Settings::install_defaults();
        Product_Post_Type::register();
        Order_Post_Type::register();
        Taxonomy::register();
        flush_rewrite_rules();
    }

    /**
     * Deactivation routine.
     */
    public static function deactivate() : void {
        flush_rewrite_rules();
    }

    /**
     * Boot runtime hooks.
     */
    public function boot() : void {
        Migrator::maybe_upgrade();
        Settings::install_defaults();
        // Public CPT/taxonomy with rewrites must wait for init ($wp_rewrite ready).
        add_action( 'init', array(Product_Post_Type::class, 'register') );
        add_action( 'init', array(Order_Post_Type::class, 'register') );
        add_action( 'init', array(Taxonomy::class, 'register') );
        Product_Post_Type::hooks();
        ( new Menu() )->register();
        ( new More_Plugins_Page() )->register();
        ( new Picking_Page() )->register();
        ( new Orders_Page() )->register();
        ( new Packing_Slip_Page() )->register();
        ( new Shipping_Label_Page() )->register();
        ( new Settings_Page() )->register();
        ( new Product_Meta_Box() )->register();
        ( new Assets() )->register();
        ( new Shortcodes() )->register();
        ( new Product_Block() )->register();
        ( new Images_Block() )->register();
        ( new Price_Block() )->register();
        ( new Buy_Block() )->register();
        ( new Return_Pages() )->register();
        ( new Order_View() )->register();
        ( new Email_Listener() )->register();
        ( new REST_Loader() )->register();
        $fs = ( function_exists( 'wpdude_dc_fs' ) ? wpdude_dc_fs() : null );
        if ( is_object( $fs ) && $fs->is__premium_only() ) {
            $this->boot_premium__premium_only();
        }
        add_filter( 'cron_schedules', array($this, 'cron_schedules') );
    }

    /**
     * Custom cron intervals.
     *
     * @param array<string, array<string, mixed>> $schedules Schedules.
     * @return array<string, array<string, mixed>>
     */
    public function cron_schedules( array $schedules ) : array {
        $schedules['wpdude_dc_five_minutes'] = array(
            'interval' => 5 * MINUTE_IN_SECONDS,
            'display'  => __( 'Every five minutes (Dude Commerce)', 'dude-commerce' ),
        );
        return $schedules;
    }

}
