<?php
add_action( 'wp_ajax_sales_landing_page_run_demo_import', 'sales_landing_page_run_demo_import' );

function sales_landing_page_run_demo_import() {

    check_ajax_referer( 'sales_landing_page_demo_nonce', 'nonce' );

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

    if ( get_option( 'sales_landing_page_demo_import_completed' ) ) {
        wp_send_json_success( 'Already imported' );
    }

    if ( ! class_exists( 'WooCommerce' ) ) {
        wp_send_json_error( 'WooCommerce not active' );
    }

    /*  CREATE PRODUCT CATEGORIES */
    $sales_landing_page_categories = array( 'Slider Products', 'Shop Products' );
    $sales_landing_page_category_ids = array();

    foreach ( $sales_landing_page_categories as $sales_landing_page_category ) {
        $sales_landing_page_term = term_exists( $sales_landing_page_category, 'product_cat' );
        if ( ! $sales_landing_page_term ) {
            $sales_landing_page_term = wp_insert_term( $sales_landing_page_category, 'product_cat' );
        }
        $sales_landing_page_category_ids[ $sales_landing_page_category ] = (int) $sales_landing_page_term['term_id'];
    }

    $sales_landing_page_img_path = get_stylesheet_directory() . '/images/';

    /*  SLIDER PRODUCTS */
    $sales_landing_page_slider_products = array(
        array(
            'title' => 'Wireless Headphone',
            'price' => '540',
            'image' => $sales_landing_page_img_path . 'banner1.png',
            'short_description' => '25% OFF – Premium wireless headphone',
            'description' => 'High-quality wireless headphone with deep bass and premium comfort.'
        ),
        array(
            'title' => 'Smart Audio Headphone',
            'price' => '520',
            'image' => $sales_landing_page_img_path . 'banner2.png',
            'short_description' => 'Noise cancelling smart headphone',
            'description' => 'Advanced sound technology with noise cancellation and long battery life.'
        ),
        array(
            'title' => 'Studio Pro Headphone',
            'price' => '560',
            'image' => $sales_landing_page_img_path . 'banner3.png',
            'short_description' => 'Studio quality audio experience',
            'description' => 'Designed for professionals with balanced sound and durable build.'
        ),
        array(
            'title' => 'Nikon HDR 90X Zoom Camera',
            'price' => '560',
            'image' => $sales_landing_page_img_path . 'banner4.png',
            'short_description' => 'Professional zoom camera',
            'description' => 'Advanced HDR camera with powerful zoom and sharp image quality.'
        ),
    );

    /*  SHOP PRODUCTS*/
    $sales_landing_page_shop_products = array(
        array(
            'title' => 'JBL Z9 Series Headphone',
            'price' => '69.99',
            'image' => $sales_landing_page_img_path . 'product-img1.png',
            'short_description' => 'High performance JBL headphone',
            'description' => 'Powerful bass and crystal clear sound for music lovers.'
        ),
        array(
            'title' => 'Unicorn Stylish T-Shirts',
            'price' => '69.99',
            'image' => $sales_landing_page_img_path . 'product-img2.png',
            'short_description' => 'Trendy casual wear',
            'description' => 'Comfortable cotton t-shirt with modern unicorn design.'
        ),
        array(
            'title' => 'Wireless JBL Ear Birds',
            'price' => '69.99',
            'image' => $sales_landing_page_img_path . 'product-img3.png',
            'short_description' => 'True wireless earbuds',
            'description' => 'Compact earbuds with fast pairing and immersive audio.'
        ),
        array(
            'title' => 'Nikon HDR 90X Zoom Camera',
            'price' => '69.99',
            'image' => $sales_landing_page_img_path . 'product-img4.png',
            'short_description' => 'Professional zoom camera',
            'description' => 'Advanced HDR camera with powerful zoom and sharp image quality.'
        ),
    );

    /*  INSERT PRODUCT FUNCTION  */
    function sales_insert_product( $sales_landing_page_product, $sales_landing_page_category_id ) {

        $sales_landing_page_product_id = wp_insert_post( array(
            'post_title'   => $sales_landing_page_product['title'],
            'post_content' => $sales_landing_page_product['description'],
            'post_excerpt' => $sales_landing_page_product['short_description'],
            'post_status'  => 'publish',
            'post_type'    => 'product',
        ) );

        if ( is_wp_error( $sales_landing_page_sproduct_id ) ) return;

        update_post_meta( $sales_landing_page_product_id, '_regular_price', $sales_landing_page_product['price'] );
        update_post_meta( $sales_landing_page_product_id, '_price', $sales_landing_page_product['price'] );
        update_post_meta( $sales_landing_page_product_id, '_stock_status', 'instock' );

        wp_set_object_terms( $sales_landing_page_product_id, $sales_landing_page_category_id, 'product_cat' );

        if ( file_exists( $sales_landing_page_product['image'] ) ) {
            $sales_landing_page_tmp = wp_tempnam( $sales_landing_page_product['image'] );
            copy( $sales_landing_page_product['image'], $sales_landing_page_tmp );

            $sales_landing_page_file = array(
                'name'     => basename( $sales_landing_page_product['image'] ),
                'tmp_name' => $sales_landing_page_tmp,
            );

            $sales_landing_page_attachment_id = media_handle_sideload( $sales_landing_page_file, $sales_landing_page_product_id );

            if ( ! is_wp_error( $sales_landing_page_attachment_id ) ) {
                set_post_thumbnail( $sales_landing_page_product_id, $sales_landing_page_attachment_id );
            }
        }
    }

    foreach ( $sales_landing_page_slider_products as $sales_landing_page_product ) {
        sales_insert_product( $sales_landing_page_product, $sales_landing_page_category_ids['Slider Products'] );
    }

    foreach ( $sales_landing_page_shop_products as $sales_landing_page_product ) {
        sales_insert_product( $sales_landing_page_product, $sales_landing_page_category_ids['Shop Products'] );
    }

    update_option( 'sales_landing_page_demo_import_completed', true );

    wp_send_json_success( 'Demo imported successfully' );
}
