<?php

if (!defined('ABSPATH')) {
    exit;
}

register_activation_hook(IAPC_PLUGIN_FILE, 'iapc_activate_plugin');

function iapc_load_plugin() {
    static $loaded = false;

    if ($loaded) {
        return;
    }

    $loaded = true;

    require_once IAPC_PLUGIN_DIR . 'includes/template.php';
    require_once IAPC_PLUGIN_DIR . 'includes/plugin.php';
    iapc_register_plugin_hooks();
}

add_action('plugins_loaded', 'iapc_load_plugin', 0);

function iapc_register_plugin_hooks() {
    add_action('plugins_loaded', 'iapc_check_woocommerce', 11);
    // Translations: WordPress.org JIT / language packs (no manual load_textdomain).

    add_action('admin_menu', 'iapc_add_admin_menu');
    add_action('admin_notices', 'iapc_render_license_save_feedback_notice', 5);
    add_action('admin_notices', 'iapc_render_admin_ai_license_notice');
    add_action('admin_enqueue_scripts', 'iapc_admin_enqueue_license_notice_styles');
    add_action('wp_enqueue_scripts', 'iapc_enqueue_assets');
    add_action('wp_enqueue_scripts', 'iapc_enqueue_cart_assets');
    add_action('admin_enqueue_scripts', 'iapc_admin_enqueue_product_scripts');

    add_shortcode('iapc_designer', 'iapc_shortcode_designer');

    add_filter('upload_mimes', 'iapc_allow_3d_uploads');
    add_filter('wp_check_filetype_and_ext', 'iapc_fix_glb_filetype', 10, 4);

    add_action('wp_ajax_iapc_generate_ai_image', 'iapc_generate_ai_image');
    add_action('wp_ajax_nopriv_iapc_generate_ai_image', 'iapc_generate_ai_image');

    add_action('admin_enqueue_scripts', 'iapc_admin_enqueue_styles');
    add_action('admin_enqueue_scripts', 'iapc_admin_page_styles');

    add_action('plugins_loaded', 'iapc_register_woocommerce_hooks', 20);
}

function iapc_register_woocommerce_hooks() {
    if (!class_exists('WooCommerce')) {
        return;
    }

    add_action('wp', 'iapc_disable_woocommerce_zoom', 100);
    add_action('woocommerce_before_add_to_cart_button', 'iapc_add_design_nonce_field');
    add_action('woocommerce_after_single_product', 'iapc_preview_overlay');
    add_action('wp', 'iapc_setup_product_page_integration', 20);
    add_action('wp', 'iapc_hide_default_product_add_to_cart', 21);
    add_action('woocommerce_before_calculate_totals', 'iapc_apply_customization_pricing');
    add_filter('woocommerce_add_to_cart_validation', 'iapc_validate_customization', 10, 3);
    add_filter('woocommerce_add_cart_item_data', 'iapc_save_cart_item_data', 10, 2);
    add_filter('woocommerce_is_purchasable', 'iapc_allow_customizer_product_purchasable', 10, 2);
    add_filter('woocommerce_product_get_price', 'iapc_customizer_empty_price_as_zero', 10, 2);
    add_filter('woocommerce_get_item_data', 'iapc_display_cart_item_data', 10, 2);
    add_filter('woocommerce_cart_item_thumbnail', 'iapc_filter_cart_item_thumbnail', 10, 3);
    add_filter('woocommerce_store_api_cart_item_images', 'iapc_filter_store_api_cart_item_images', 10, 3);
    add_action('woocommerce_checkout_create_order_line_item', 'iapc_save_order_line_item_data', 10, 4);
    add_action('woocommerce_product_options_general_product_data', 'iapc_add_product_type_field');
    add_action('woocommerce_after_order_itemmeta', 'iapc_display_order_images_admin_only', 10, 3);
    add_filter('woocommerce_hidden_order_itemmeta', 'iapc_hide_order_itemmeta');
    add_action('woocommerce_process_product_meta', 'iapc_save_product_type_field', 20, 1);
}
