<?php
/**
 * Plugin Name: Maple Carts
 * Plugin URI: https://mapleopentech.ca/maple-carts
 * Description: Lightweight abandoned cart recovery for WooCommerce. No bloat, no tracking, no upsells.
 * Version: 1.0.0
 * Author: Maple Open Tech
 * Author URI: https://mapleopentech.ca
 * Text Domain: maple-carts
 * Domain Path: /languages
 * Requires at least: 5.8
 * Requires PHP: 7.4
 * WC requires at least: 5.0
 * WC tested up to: 9.0
 * Requires Plugins: woocommerce
 * License: GPL v2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 *
 * @package MapleCarts
 */

defined( 'ABSPATH' ) || exit;

// Plugin constants.
define( 'MAPLE_CARTS_VERSION', '1.0.0' );
define( 'MAPLE_CARTS_FILE', __FILE__ );
define( 'MAPLE_CARTS_PATH', plugin_dir_path( __FILE__ ) );
define( 'MAPLE_CARTS_URL', plugin_dir_url( __FILE__ ) );

// Database table names (without prefix).
define( 'MAPLE_CARTS_TABLE', 'maple_carts' );
define( 'MAPLE_CARTS_EMAILS_TABLE', 'maple_carts_emails' );
define( 'MAPLE_CARTS_EMAIL_LOG_TABLE', 'maple_carts_email_log' );

/**
 * Main plugin class.
 */
final class Maple_Carts {

    /**
     * Single instance.
     *
     * @var Maple_Carts
     */
    private static $instance = null;

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

    /**
     * Constructor.
     */
    private function __construct() {
        $this->includes();
        $this->init_hooks();
    }

    /**
     * Include required files.
     */
    private function includes() {
        require_once MAPLE_CARTS_PATH . 'includes/class-maple-carts-db.php';
        require_once MAPLE_CARTS_PATH . 'includes/class-maple-carts-tracking.php';
        require_once MAPLE_CARTS_PATH . 'includes/class-maple-carts-emails.php';
        require_once MAPLE_CARTS_PATH . 'includes/class-maple-carts-privacy.php';
        require_once MAPLE_CARTS_PATH . 'includes/class-maple-carts-mailjet.php';
        
        if ( is_admin() ) {
            require_once MAPLE_CARTS_PATH . 'includes/class-maple-carts-admin.php';
        }
    }

    /**
     * Initialize hooks.
     */
    private function init_hooks() {
        register_activation_hook( MAPLE_CARTS_FILE, [ 'Maple_Carts_DB', 'activate' ] );
        register_deactivation_hook( MAPLE_CARTS_FILE, [ $this, 'deactivate' ] );

        add_action( 'plugins_loaded', [ $this, 'on_plugins_loaded' ] );
        add_action( 'before_woocommerce_init', [ $this, 'declare_hpos_compatibility' ] );
    }

    /**
     * On plugins loaded.
     */
    public function on_plugins_loaded() {
        if ( ! class_exists( 'WooCommerce' ) ) {
            add_action( 'admin_notices', [ $this, 'woocommerce_missing_notice' ] );
            return;
        }

        // Initialize components.
        Maple_Carts_Tracking::instance();
        Maple_Carts_Emails::instance();
        Maple_Carts_Privacy::instance();
        Maple_Carts_Mailjet::instance();
        
        if ( is_admin() ) {
            Maple_Carts_Admin::instance();
        }

        // Load textdomain.
        load_plugin_textdomain( 'maple-carts', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
    }

    /**
     * Deactivation.
     */
    public function deactivate() {
        wp_clear_scheduled_hook( 'maple_carts_send_emails' );
        wp_clear_scheduled_hook( 'maple_carts_cleanup' );
    }

    /**
     * HPOS compatibility.
     */
    public function declare_hpos_compatibility() {
        if ( class_exists( '\Automattic\WooCommerce\Utilities\FeaturesUtil' ) ) {
            \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', MAPLE_CARTS_FILE, true );
        }
    }

    /**
     * WooCommerce missing notice.
     */
    public function woocommerce_missing_notice() {
        ?>
        <div class="notice notice-error">
            <p><?php esc_html_e( 'Maple Carts requires WooCommerce to be installed and active.', 'maple-carts' ); ?></p>
        </div>
        <?php
    }

    /**
     * Get plugin option.
     *
     * @param string $key     Option key.
     * @param mixed  $default Default value.
     * @return mixed
     */
    public static function get_option( $key, $default = '' ) {
        $options = get_option( 'maple_carts_settings', [] );
        return isset( $options[ $key ] ) ? $options[ $key ] : $default;
    }

    /**
     * Update plugin option.
     *
     * @param string $key   Option key.
     * @param mixed  $value Value.
     */
    public static function update_option( $key, $value ) {
        $options = get_option( 'maple_carts_settings', [] );
        $options[ $key ] = $value;
        update_option( 'maple_carts_settings', $options );
    }
}

/**
 * Main instance.
 *
 * @return Maple_Carts
 */
function maple_carts() {
    return Maple_Carts::instance();
}

// Initialize.
maple_carts();
