<?php
/**
 * Demo Import - Jewelry Market
 */

add_action('wp_ajax_jewelry_market_run_demo_import', 'jewelry_market_run_demo_import');

function jewelry_market_run_demo_import() {
    while (ob_get_level()) {
        ob_end_clean();
    }

    check_ajax_referer('jewelry_market_demo_nonce', 'nonce');

    if (!current_user_can('manage_options')) {
        wp_send_json_error('Permission denied');
    }

    if (get_option('jewelry_market_demo_import_completed')) {
        wp_send_json_success('Already Imported');
    }

    // Required files
    require_once ABSPATH . 'wp-admin/includes/image.php';
    require_once ABSPATH . 'wp-admin/includes/file.php';
    require_once ABSPATH . 'wp-admin/includes/media.php';

    // Categories + Products Data
    $jewelry_market_categories = [

        "Eternal Diamond Collection" => [
            "description" => "Brilliant diamond rings, necklaces, and earrings crafted for elegance.",
            "image" => get_template_directory_uri() . '/assets/images/service01.png',
            "products" => [
                [
                    "title" => "Diamond Solitaire Ring",
                    "image" => get_template_directory_uri() . '/assets/images/service01.png'
                ],
                [
                    "title" => "Diamond Necklace",
                    "image" => get_template_directory_uri() . '/assets/images/service02.png'
                ],
                [
                    "title" => "Diamond Earrings",
                    "image" => get_template_directory_uri() . '/assets/images/service03.png'
                ]
            ]
        ],

        "Bridal Radiance" => [
            "description" => "Brilliant diamond rings, necklaces, and earrings crafted for elegance.",
            "image" => get_template_directory_uri() . '/assets/images/service02.png',
            "products" => [
                [
                    "title" => "Bridal Gold Necklace Set",
                    "image" => get_template_directory_uri() . '/assets/images/service02.png'
                ],
                [
                    "title" => "Bridal Gold Earrings",
                    "image" => get_template_directory_uri() . '/assets/images/service01.png'
                ],
                [
                    "title" => "Bridal Gold Ring",
                    "image" => get_template_directory_uri() . '/assets/images/service04.png'
                ]
            ]
        ],

        "Modern Minimal" => [
            "description" => "Brilliant diamond rings, necklaces, and earrings crafted for elegance.",
            "image" => get_template_directory_uri() . '/assets/images/service03.png',
            "products" => [
                [
                    "title" => "Minimalist Silver Pendant",
                    "image" => get_template_directory_uri() . '/assets/images/service02.png'
                ],
                [
                    "title" => "Minimal Silver Ring",
                    "image" => get_template_directory_uri() . '/assets/images/service04.png'
                ],
                [
                    "title" => "Minimal Chain Necklace",
                    "image" => get_template_directory_uri() . '/assets/images/service02.png' // fixed name
                ]
            ]
        ],

        "Golden Heritage" => [
            "description" => "Rich gold jewellery inspired by classic artistry.",
            "image" => get_template_directory_uri() . '/assets/images/service04.png',
            "products" => [
                [
                    "title" => "Traditional Gold Bangle Set",
                    "image" => get_template_directory_uri() . '/assets/images/service03.png'
                ],
                [
                    "title" => "Gold Temple Necklace",
                    "image" => get_template_directory_uri() . '/assets/images/service02.png'
                ],
                [
                    "title" => "Antique Gold Earrings",
                    "image" => get_template_directory_uri() . '/assets/images/service01.png'
                ]
            ]
        ]

    ];

    $jewelry_market_default_description = "Premium quality product for your daily care.";

    foreach ($jewelry_market_categories as $jewelry_market_cat_name => $jewelry_market_cat_data) {

        // Create Category
        $jewelry_market_term = term_exists($jewelry_market_cat_name, 'product_cat');

        if (!$jewelry_market_term) {
            $jewelry_market_term = wp_insert_term($jewelry_market_cat_name, 'product_cat', [
                'description' => $jewelry_market_cat_data['description']
            ]);
        }

        if (is_wp_error($jewelry_market_term)) continue;

        $jewelry_market_cat_id = (int) $jewelry_market_term['term_id'];

        // Category Image
        $jewelry_market_image_id = media_sideload_image(
            $jewelry_market_cat_data['image'],
            0,
            null,
            'id'
        );

        if (!is_wp_error($jewelry_market_image_id)) {
            update_term_meta($jewelry_market_cat_id, 'thumbnail_id', $jewelry_market_image_id);
        }

        // Create Products
        foreach ($jewelry_market_cat_data['products'] as $jewelry_market_prod) {

            if (!get_page_by_title($jewelry_market_prod['title'], OBJECT, 'product')) {

                $jewelry_market_post_id = wp_insert_post([
                    'post_title'   => $jewelry_market_prod['title'],
                    'post_content' => $jewelry_market_default_description,
                    'post_status'  => 'publish',
                    'post_type'    => 'product'
                ]);

                if (is_wp_error($jewelry_market_post_id)) continue;

                // Product Meta
                update_post_meta($jewelry_market_post_id, '_price', '100');
                update_post_meta($jewelry_market_post_id, '_regular_price', '100');
                update_post_meta($jewelry_market_post_id, '_stock_status', 'instock');
                update_post_meta($jewelry_market_post_id, '_manage_stock', 'no');
                update_post_meta($jewelry_market_post_id, '_visibility', 'visible');

                // Product Type
                wp_set_object_terms($jewelry_market_post_id, 'simple', 'product_type');

                // Category Assign
                wp_set_object_terms($jewelry_market_post_id, [$jewelry_market_cat_id], 'product_cat');

                // Product Image
                $jewelry_market_product_img_id = media_sideload_image(
                    $jewelry_market_prod['image'],
                    $jewelry_market_post_id,
                    null,
                    'id'
                );

                if (!is_wp_error($jewelry_market_product_img_id)) {
                    set_post_thumbnail($jewelry_market_post_id, $jewelry_market_product_img_id);
                }
            }
        }
    }

    update_option('jewelry_market_demo_import_completed', true);

    wp_send_json_success('Demo Imported Successfully');
}