<?php

defined( 'ABSPATH' ) || exit;

use Vendtics\WpMVC\App;

/**
 * Plugin Name:       Vendtics
 * Description:       Email marketing solution for Newsletters, Campaigns, and Automation.
 * Version:           0.0.4
 * Requires at least: 6.2
 * Requires PHP:      7.4
 * Tested up to:      6.7
 * Author:            TemplatePath
 * Author URI:        https://vendtics.com
 * License:           GPL v3 or later
 * License URI:       http://www.gnu.org/licenses/gpl-3.0.html
 * Text Domain:       vendtics
 * Domain Path:       /languages
 */

require_once __DIR__ . '/vendor/vendor-src/autoload.php';
require_once __DIR__ . '/app/Helpers/helper.php';

final class Vendtics
{
    public static Vendtics $instance;

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

    public function load() {

        // Run Activation Tasks
        register_activation_hook(
            __FILE__, function() {
                ( new Vendtics\Database\Migrations\CreateDB )->execute();
            } 
        );

        $application = App::instance();

        $application->boot( __FILE__, __DIR__ );

        register_deactivation_hook( __FILE__, [$this, 'clear_cron'] );

        /**
         * Fires once activated plugins have loaded.
         *
         */
        add_action(
            'plugins_loaded', function () use ( $application ): void {

                do_action( 'vendtics_before_load' );

                $application->load();

                do_action( 'vendtics_after_load' );
            }
        );
    }

    public function clear_cron() {
        $timestamp = wp_next_scheduled( 'vendtics_every_min_cron_job_event' );
        wp_unschedule_event( $timestamp, 'vendtics_every_min_cron_job_event' );
    }
}

Vendtics::instance()->load();