<?php
/**
 * Plugin Name: Unit Converter & Sizing Calculator for Product Pages
 * Plugin URI: https://www.calcslive.com/services/embed
 * Description: Add unit converters to product specs and embed sizing calculators that help customers buy the right size and quantity. Powered by CalcsLive.
 * Version: 1.3.2
 * Author: CalcsLive
 * Author URI: https://www.calcslive.com
 * License: GPL v2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: calcslive-article-embed
 * Domain Path: /languages
 * Requires at least: 5.8
 * Requires PHP: 7.4
 */

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

// Plugin constants
define('CALCSLIVE_VERSION', '1.3.2');
define('CALCSLIVE_PQ_VERSION', '1.3.8');
define('CALCSLIVE_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('CALCSLIVE_PLUGIN_URL', plugin_dir_url(__FILE__));
define('CALCSLIVE_EMBED_BASE_URL', 'https://www.calcslive.com/embed/');

/**
 * Main plugin class
 */
class CalcsLive_Article_Embed {

    /**
     * Single instance of the class
     */
    private static $instance = null;

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

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

    /**
     * Load required files
     */
    private function load_dependencies() {
        require_once CALCSLIVE_PLUGIN_DIR . 'includes/class-calcslive-settings.php';
        require_once CALCSLIVE_PLUGIN_DIR . 'includes/class-calcslive-shortcode.php';
        require_once CALCSLIVE_PLUGIN_DIR . 'includes/class-calcslive-block.php';
        require_once CALCSLIVE_PLUGIN_DIR . 'includes/class-calcslive-pq-shortcode.php';
    }

    /**
     * Initialize hooks
     */
    private function init_hooks() {
        // Initialize settings
        add_action('admin_menu', array('CalcsLive_Settings', 'add_admin_menu'));
        add_action('admin_init', array('CalcsLive_Settings', 'register_settings'));
        add_action('admin_enqueue_scripts', array('CalcsLive_Settings', 'enqueue_admin_scripts'));
        add_action('wp_ajax_calcslive_test_token', array('CalcsLive_Settings', 'ajax_test_token'));

        // Initialize shortcode
        add_action('init', array('CalcsLive_Shortcode', 'register'));

        // Initialize Gutenberg block
        add_action('init', array('CalcsLive_Block', 'register'));

        // Initialize PQ shortcodes
        add_action('init', array('CalcsLive_PQ_Shortcode', 'register'));

        // Enqueue frontend styles
        add_action('wp_enqueue_scripts', array($this, 'enqueue_frontend_styles'));

        // Enqueue admin styles
        add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_styles'));

        // Add settings link to plugins page
        add_filter('plugin_action_links_' . plugin_basename(__FILE__), array($this, 'add_settings_link'));
    }

    /**
     * Enqueue frontend styles
     */
    public function enqueue_frontend_styles() {
        wp_enqueue_style(
            'calcslive-article-embed',
            CALCSLIVE_PLUGIN_URL . 'assets/frontend.css',
            array(),
            CALCSLIVE_VERSION
        );
    }

    /**
     * Enqueue admin styles
     */
    public function enqueue_admin_styles($hook) {
        if ('settings_page_calcslive-settings' !== $hook) {
            return;
        }

        wp_enqueue_style(
            'calcslive-article-embed-admin',
            CALCSLIVE_PLUGIN_URL . 'assets/admin.css',
            array(),
            CALCSLIVE_VERSION
        );
    }

    /**
     * Add settings link to plugins page
     */
    public function add_settings_link($links) {
        $settings_link = '<a href="' . admin_url('options-general.php?page=calcslive-settings') . '">' . __('Settings', 'calcslive-article-embed') . '</a>';
        array_unshift($links, $settings_link);
        return $links;
    }
}

/**
 * Plugin activation hook
 */
function calcslive_activate() {
    // Set default options
    if (false === get_option('calcslive_api_key')) {
        add_option('calcslive_api_key', '');
    }
    if (false === get_option('calcslive_default_width')) {
        add_option('calcslive_default_width', '100%');
    }
    if (false === get_option('calcslive_default_height')) {
        add_option('calcslive_default_height', '600');
    }
    if (false === get_option('calcslive_domain')) {
        // Try to auto-detect domain
        $site_url = get_site_url();
        $parsed = wp_parse_url($site_url);
        $domain = isset($parsed['host']) ? $parsed['host'] : '';
        add_option('calcslive_domain', $domain);
    }
}
register_activation_hook(__FILE__, 'calcslive_activate');

/**
 * Plugin deactivation hook
 */
function calcslive_deactivate() {
    // Clean up if needed
}
register_deactivation_hook(__FILE__, 'calcslive_deactivate');

/**
 * Initialize the plugin
 */
function calcslive_init() {
    CalcsLive_Article_Embed::get_instance();
}
add_action('plugins_loaded', 'calcslive_init');
