<?php
if (!defined('ABSPATH'))
    exit; // Exit if accessed directly

function dspp_custom_services_post_type() {
    $labels = array(
        'name' => _x('DSP Services', 'post type general name', 'digital-service-provider-crm'),
        'singular_name' => _x('DSP Service', 'post type singular name', 'digital-service-provider-crm'),
        'menu_name' => _x('DSP Services', 'admin menu', 'digital-service-provider-crm'),
        'name_admin_bar' => _x('DSP Service', 'add new on admin bar', 'digital-service-provider-crm'),
        'add_new' => _x('Add New', 'Service', 'digital-service-provider-crm'),
        'add_new_item' => __('Add New Service', 'digital-service-provider-crm'),
        'new_item' => __('New Service', 'digital-service-provider-crm'),
        'edit_item' => __('Edit Service', 'digital-service-provider-crm'),
        'view_item' => __('View Service', 'digital-service-provider-crm'),
        'all_items' => __('All Services', 'digital-service-provider-crm'),
        'search_items' => __('Search Services', 'digital-service-provider-crm'),
        'parent_item_colon' => __('Parent Services:', 'digital-service-provider-crm'),
        'not_found' => __('No Services found.', 'digital-service-provider-crm'),
        'not_found_in_trash' => __('No Services found in Trash.', 'digital-service-provider-crm')
    );
    $args = array(
        'labels' => $labels,
        'description' => __('Description.', 'digital-service-provider-crm'),
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'query_var' => true,
        'rewrite' => array('slug' => 'dspp-service'),
        'capability_type' => 'post',
        'has_archive' => true,
        'hierarchical' => false,
        'menu_position' => null,
        'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments'),
        'taxonomies' => array('dspp_service_category'), // Custom taxonomy for your custom post type
    );
    register_post_type('dspp_service', $args);
    // Register custom taxonomy for your custom post type
    $taxonomy_args = array(
        'hierarchical' => true,
        'labels' => array(
            'name' => _x('Service Categories', 'Services Category Texonomy', 'digital-service-provider-crm'),
            'singular_name' => _x('Service Category', 'Services Category Texonomy', 'digital-service-provider-crm'),
            'search_items' => __('Search Service Categories', 'digital-service-provider-crm'),
            'all_items' => __('All Service Categories', 'digital-service-provider-crm'),
            'parent_item' => __('Parent Service Category', 'digital-service-provider-crm'),
            'parent_item_colon' => __('Parent Service Category:', 'digital-service-provider-crm'),
            'edit_item' => __('Edit Service Category', 'digital-service-provider-crm'),
            'update_item' => __('Update Service Category', 'digital-service-provider-crm'),
            'add_new_item' => __('Add New Service Category', 'digital-service-provider-crm'),
            'new_item_name' => __('New Service Category Name', 'digital-service-provider-crm'),
            'menu_name' => __('Service Categories', 'digital-service-provider-crm'),
        ),
        'rewrite' => array('slug' => 'dspp-services-category'),
    );

    register_taxonomy('dspp_services_category', 'dspp_service', $taxonomy_args);
}

add_action('init', 'dspp_custom_services_post_type');

// Add custom meta box for the "Price" field
//function dspp_add_service_price_meta_box() {
//    add_meta_box(
//            'dspp_service_price_meta_box',
//            'Price',
//            'dspp_service_price_meta_box_callback',
//            'dspp_service', // Replace with your custom post type
//            'normal',
//            'high'
//    );
//}
//
//add_action('add_meta_boxes', 'dspp_add_service_price_meta_box');

function dspp_add_service_price_meta_box() {
    add_meta_box(
            'dspp_service_price_meta_box',
            'Service Pricing',
            'dspp_service_price_meta_box_callback',
            'dspp_service',
            'normal',
            'high'
    );
}

add_action('add_meta_boxes', 'dspp_add_service_price_meta_box');

function dspp_service_price_meta_box_callback($post) {
    $service_type = get_post_meta($post->ID, '_dspp_service_type', true) ?: 'single';
    $single_price = get_post_meta($post->ID, '_dspp_single_price', true);
    $attributes = get_post_meta($post->ID, '_dspp_attributes', true) ?: [];
    $variations = get_post_meta($post->ID, '_dspp_variations', true) ?: [];
    ?>

    <label><input type="radio" name="dspp_service_type" value="single" <?php checked($service_type, 'single'); ?> /> Single Service</label>
    <label style="margin-left: 20px;"><input type="radio" name="dspp_service_type" value="variable" <?php checked($service_type, 'variable'); ?> /> Variable Service</label>

    <div id="dspp_single_price" style="margin-top: 15px; <?php echo $service_type === 'single' ? '' : 'display:none;'; ?>">
        <label>Price:</label>
        <input type="number" name="dspp_single_price" value="<?php echo esc_attr($single_price); ?>" step="0.01" />
    </div>
    <div id="dspp_variable_section" style="margin-top: 15px; <?php echo $service_type === 'variable' ? '' : 'display:none;'; ?>">
        <?php
            $pro_enabled = dspp_check_api_connection_settings();
            if (!$pro_enabled) {
                ?>
                <div class="dspp-disabled-section">
                    <strong>Premium Features (Disabled)</strong>
                    <p>Upgrade to unlock these features. Visit our <a href="<?php echo DSPP_WEB_API_URL; ?>" target="_blank" style="pointer-events: auto;">website</a> for more info.</p>
                </div>
                <?php
            }
        ?>
        <h4 class="<?php echo ($pro_enabled) ? '' : 'dspp-disabled-content' ?>">Attributes</h4>
        <div id="dspp_attributes" class="<?php echo ($pro_enabled) ? '' : 'dspp-disabled-content' ?>">
            <?php foreach ($attributes as $index => $attr): ?>
                <div class="attribute-item">
                    <table>
                        <tr>
                            <td><input type="text" name="dspp_attributes[<?php echo $index; ?>][name]" placeholder="Attribute name" value="<?php echo esc_attr($attr['name']); ?>" /></td>
                            <td><input type="text" name="dspp_attributes[<?php echo $index; ?>][values]" placeholder="Comma-separated values" value="<?php echo esc_attr(implode(',', $attr['values'])); ?>" /></td>
                            <td><button type="button" class="button button-primary button-large remove-attribute <?php echo ($pro_enabled) ? '' : 'dspp-disabled-content' ?>">Remove</button></td>
                        </tr>
                    </table>
                </div>
            <?php endforeach; ?>
        </div>
        <button type="button" class="button button-primary button-large <?php echo ($pro_enabled) ? '' : 'dspp-disabled-content' ?>" id="add-attribute">Add Attribute</button>

        <h4 style="margin-top: 20px;" class="<?php echo ($pro_enabled) ? '' : 'dspp-disabled-content' ?>">Variations</h4>
        <div id="dspp_variations" class="<?php echo ($pro_enabled) ? '' : 'dspp-disabled-content' ?>">
            <?php foreach ($variations as $index => $variation): ?>
                <table>    
                    <tr class="variation-item">
                        <td style="width: 200px;">
                            <?php foreach ($variation['attributes'] as $attr => $val): ?>
                                <strong><?php echo esc_html($attr); ?>:</strong> 
                                <?php echo esc_html($val); ?>&nbsp;
                            <?php endforeach; ?>
                        </td>
                        <td><input type="hidden" name="dspp_variations[<?php echo $index; ?>][attributes]" value='<?php echo json_encode($variation["attributes"]); ?>' /></td>
                        <td><input type="number" name="dspp_variations[<?php echo $index; ?>][price]" step="0.01" value="<?php echo esc_attr($variation['price']); ?>" placeholder="Price" /></td>
                    </tr>
                </table>
            <?php endforeach; ?>
        </div>
        <button type="button" class="button button-primary button-large <?php echo ($pro_enabled) ? '' : 'dspp-disabled-content' ?>" id="generate-variations" style="margin-top: 15px;">Generate Variations</button>
    </div>

    <style>
        .attribute-item, .variation-item {
            margin-bottom: 10px;
        }
        .attribute-item input {
            margin-right: 10px;
        }
    </style>

    <script>
        document.addEventListener('DOMContentLoaded', function () {
            const typeRadios = document.querySelectorAll('input[name="dspp_service_type"]');
            const singleDiv = document.getElementById('dspp_single_price');
            const variableDiv = document.getElementById('dspp_variable_section');

            typeRadios.forEach(radio => {
                radio.addEventListener('change', function () {
                    if (this.value === 'single') {
                        singleDiv.style.display = '';
                        variableDiv.style.display = 'none';
                    } else {
                        singleDiv.style.display = 'none';
                        variableDiv.style.display = '';
                    }
                });
            });

            const attrContainer = document.getElementById('dspp_attributes');
            const addAttrBtn = document.getElementById('add-attribute');
            const genVarBtn = document.getElementById('generate-variations');
            const variationContainer = document.getElementById('dspp_variations');

            addAttrBtn.addEventListener('click', function () {
                const index = attrContainer.children.length;
                const div = document.createElement('div');
                div.classList.add('attribute-item');
                div.innerHTML = `<table><tr><td>
                <input type="text" name="dspp_attributes[${index}][name]" placeholder="Attribute name" /></td><td>
                <input type="text" name="dspp_attributes[${index}][values]" placeholder="Comma-separated values" /></td><td>
                <button type="button" class="button button-primary remove-attribute">Remove</button></td></tr></table>
            `;
                attrContainer.appendChild(div);
            });

            attrContainer.addEventListener('click', function (e) {
                if (e.target.classList.contains('remove-attribute')) {
                    e.target.parentElement.parentElement.parentElement.remove();
                }
            });

            genVarBtn.addEventListener('click', function () {
                const attrItems = document.querySelectorAll('.attribute-item');
                const attributes = {};
                attrItems.forEach(item => {
                    const nameInput = item.querySelector('input[name*="[name]"]');
                    const valuesInput = item.querySelector('input[name*="[values]"]');
                    const key = nameInput.value.trim();
                    const vals = valuesInput.value.split(',').map(v => v.trim()).filter(Boolean);
                    if (key && vals.length) {
                        attributes[key] = vals;
                    }
                });
                const keys = Object.keys(attributes);
                if (!keys.length)
                    return;
                function generateCombos(attrMap) {
                    return Object.keys(attrMap).reduce((acc, key) => {
                        const vals = attrMap[key];
                        if (!acc.length)
                            return vals.map(val => ({[key]: val}));
                        return acc.flatMap(prev =>
                            vals.map(val => ({...prev, [key]: val}))
                        );
                    }, []);
                }
                const combos = generateCombos(attributes);
                variationContainer.innerHTML = '';
                combos.forEach((combo, i) => {
                    const inputAttrs = `<input type="hidden" name="dspp_variations[${i}][attributes]" value='${JSON.stringify(combo)}' />`;
                    const labels = Object.entries(combo).map(([k, v]) => `<strong>${k}</strong>: ${v}`).join(' ');
                    variationContainer.innerHTML += `
                    <table><tr class="variation-item">
                        <td style="width: 200px;">${labels}</td>
                        <td>${inputAttrs}</td>
                        <td><input type="number" name="dspp_variations[${i}][price]" step="0.01" placeholder="Price" /></td>
                    </tr></table>`;
                });
            });
        });
    </script>
    <?php
}

add_action('save_post_dspp_service', function ($post_id) {
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
        return;

    $type = $_POST['dspp_service_type'] ?? 'single';
    update_post_meta($post_id, '_dspp_service_type', $type);

    if ($type === 'single') {
        $price = sanitize_text_field($_POST['dspp_single_price']);
        update_post_meta($post_id, '_dspp_single_price', $price);
        delete_post_meta($post_id, '_dspp_attributes');
        delete_post_meta($post_id, '_dspp_variations');
    } else {
        // Save attributes
        $attributes_raw = $_POST['dspp_attributes'] ?? [];
        $attributes_clean = [];
        foreach ($attributes_raw as $attr) {
            $name = sanitize_text_field($attr['name'] ?? '');
            $values = explode(',', sanitize_text_field($attr['values'] ?? ''));
            $values = array_map('trim', array_filter($values));
            if ($name && $values) {
                $attributes_clean[] = ['name' => $name, 'values' => $values];
            }
        }
        update_post_meta($post_id, '_dspp_attributes', $attributes_clean);

        // Save variations
        $variations_raw = $_POST['dspp_variations'] ?? [];
        $variations_clean = [];
        foreach ($variations_raw as $var) {
            $attrs = json_decode(stripslashes($var['attributes']), true);
            $price = floatval($var['price']);
            if (is_array($attrs)) {
                $variations_clean[] = [
                    'attributes' => $attrs,
                    'price' => $price,
                ];
            }
        }
        update_post_meta($post_id, '_dspp_variations', $variations_clean);
        delete_post_meta($post_id, '_dspp_single_price');
    }
});

// Save the "Price" field when the post is saved
//function dspp_save_service_price_meta_box($post_id) {
//    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE || !isset($_POST['service_price_nonce'])) {
//        return;
//    }
//    if (!isset( $_POST['service_price_nonce'] ) && !wp_verify_nonce( sanitize_text_field( wp_unslash ( $_POST['service_price_nonce'])) , 'save_service_price')){
//        return;
//    }
//    if (!current_user_can('edit_post', $post_id))
//        return;
//
//    // Save the "Price" field value
//    if (isset($_POST['service_price'])) {
//        update_post_meta($post_id, '_service_price', sanitize_text_field($_POST['service_price']));
//    }
//}
//add_action('save_post', 'dspp_save_service_price_meta_box');
// Add custom columns to the admin screen
function dspp_custom_service_columns($columns) {
    $columns['services_category'] = 'Category';
    $columns['price'] = 'Price';
    return $columns;
}

add_filter('manage_dspp_service_posts_columns', 'dspp_custom_service_columns');

// Populate custom columns with data
function dspp_custom_service_column_content($column, $post_id) {
    switch ($column) {
        case 'services_category':
            $category = get_the_terms($post_id, 'dspp_services_category'); // Replace with your actual taxonomy name
            echo!empty($category) ? esc_html($category[0]->name) : 'N/A';
            break;

        case 'price':
            $price = get_post_meta($post_id, '_service_price', true);
            echo!empty($price) ? esc_html($price) : 'N/A';
            break;
    }
}

add_action('manage_dspp_service_posts_custom_column', 'dspp_custom_service_column_content', 10, 2);

function dspp_add_to_session() {
    if (isset($_POST['nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'add_to_session_nonce')) {
        $response = array();
        $data_to_add = array();
//        print_r($_POST);
//        exit;
        $site = sanitize_text_field($_POST['site']);
        $user_id_wp = sanitize_key($_POST['user_id']);
        $price = sanitize_key($_POST['price']);
        $post_id = sanitize_key($_POST['post_id']);
        $variation_id = sanitize_key($_POST['data']['variation_id']);
        $site_variation = $site . '_' . $variation_id;
        $data_to_add ['site'] = $site;
        $data_to_add ['price'] = $price;
        $data_to_add ['user_id'] = $user_id_wp;
        $data_to_add ['post_id'] = $post_id;
        $data_to_add ['variation_id'] = $variation_id;
        $custom_website_data = get_user_meta($user_id_wp, 'custom_website_data', true);
        $decoded_array = json_decode($custom_website_data, true);
//    $total_price = (isset($decoded_array['custom_website_data_' . $user_id_wp]['discount_price']) && !empty($decoded_array['custom_website_data_' . $user_id_wp]['discount_price'])) ? $data_to_add['total_price'] + $decoded_array['custom_website_data_' . $user_id_wp]['discount_price'] : $data_to_add['total_price'];
        $data_array = (isset($decoded_array) && !empty($decoded_array)) ? $decoded_array : array();
        if (isset($data_array['custom_website_data_' . $user_id_wp]["$site_variation"])) {
            $current = $data_array['custom_website_data_' . $user_id_wp]["$site_variation"]['quantity'] = 1;
            $data_array['custom_website_data_' . $user_id_wp]["$site_variation"]['quantity'] = $current + 1;
            $encoded_array = wp_json_encode($data_array);
            update_user_meta($user_id_wp, 'custom_website_data', $encoded_array);
            $response = array("status" => true, "message" => "Quantity updated successfully.");
        } else {
            $data_array['custom_website_data_' . $user_id_wp]["$site_variation"] = $data_to_add;
            $data_array['custom_website_data_' . $user_id_wp]["$site_variation"]['quantity'] = 1;
            $encoded_array = wp_json_encode($data_array);
            update_user_meta($user_id_wp, 'custom_website_data', $encoded_array);
            $response = array("status" => true, "message" => "Website added to cart successfully.");
        }
        echo wp_json_encode($response);
    } else {
        wp_send_json_error('Nonce verification failed');
    }
    wp_die();
}

add_action('wp_ajax_dspp_add_to_session', 'dspp_add_to_session'); // For logged-in users
add_action('wp_ajax_nopriv_dspp_add_to_session', 'dspp_add_to_session'); // For non-logged-in users

function dspp_update_quantity_in_session() {
    if (isset($_POST['nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'update_quantity_in_session')) {
        $response = array();
        $data_to_add = array();
        $site = sanitize_text_field($_POST['site']);
        $user_id_wp = sanitize_key($_POST['user_id']);
        $price = sanitize_key($_POST['price']);
        $post_id = sanitize_key($_POST['post_id']);
        $value = sanitize_text_field($_POST['value']);
        $variation_id = sanitize_key($_POST['variation_id']);
        $site_variation = $site . '_' . $variation_id;
        $data_to_add ['site'] = $site_variation;
        $data_to_add ['price'] = $price;
        $data_to_add ['user_id'] = $user_id_wp;
        $data_to_add ['post_id'] = $post_id;
        $data_to_add ['value'] = $value;
        $custom_website_data = get_user_meta($user_id_wp, 'custom_website_data', true);
        $decoded_array = json_decode($custom_website_data, true);
        $data_array = (isset($decoded_array) && !empty($decoded_array)) ? $decoded_array : array();
        if (isset($data_array['custom_website_data_' . $user_id_wp]["$site_variation"])) {
            $data_array['custom_website_data_' . $user_id_wp]["$site_variation"]['quantity'] = $data_to_add['value'];
            $encoded_array = wp_json_encode($data_array);
            update_user_meta($user_id_wp, 'custom_website_data', $encoded_array);
            $response = array("status" => true, "message" => "Quantity updated successfully.");
        }
        echo wp_json_encode($response);
    } else {
        wp_send_json_error('Nonce verification failed');
    }
    wp_die();
}

add_action('wp_ajax_dspp_update_quantity_in_session', 'dspp_update_quantity_in_session'); // For logged-in users
add_action('wp_ajax_nopriv_dspp_update_quantity_in_session', 'dspp_update_quantity_in_session'); // For non-logged-in users

function dspp_hasSubarray($array) {
    foreach ($array as $key => $value) {
        if (is_array($value)) {
            return true;
        }
    }
    return false;
}

function dspp_remove_from_session() {
    if (isset($_POST['nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'remove_from_session')) {
        $site = sanitize_text_field($_POST['website_url']);
        $variation_id = sanitize_text_field($_POST['variation_id']);
        $website_url = $site . '_' . $variation_id;
        $user_id_wp = sanitize_key($_POST['user_id']);
        $custom_website_data = get_user_meta($user_id_wp, 'custom_website_data', true);
        $decoded_array = json_decode($custom_website_data, true);
        $data_array = (isset($decoded_array) && !empty($decoded_array)) ? $decoded_array : array();
//    $total_price = (isset($data_array['custom_website_data_' . $user_id_wp]['discount_price']) && !empty($data_array['custom_website_data_' . $user_id_wp]['discount_price'])) ? $_POST['total_price'] + $data_array['custom_website_data_' . $user_id_wp]['discount_price'] : $_POST['total_price'];
        if (isset($data_array['custom_website_data_' . $user_id_wp][$website_url])) {
            unset($data_array['custom_website_data_' . $user_id_wp][$website_url]);
//        $data_array['custom_website_data_' . $user_id_wp]["total_price"] = $total_price;
            $hasSubarray = dspp_hasSubarray($data_array['custom_website_data_' . $user_id_wp]);
            if (!$hasSubarray) {
                unset($data_array['custom_website_data_' . $user_id_wp]['coupon_code']);
                unset($data_array['custom_website_data_' . $user_id_wp]['coupon_value']);
                unset($data_array['custom_website_data_' . $user_id_wp]['discount_price']);
                unset($data_array['custom_website_data_' . $user_id_wp]['coupon_discount_type']);
            }
            $encoded_array = wp_json_encode($data_array);
            update_user_meta($user_id_wp, 'custom_website_data', $encoded_array);
            $response = array("status" => true, "message" => "Item removed from cart successfully.");
        } else {
            $response = array("status" => false, "message" => "Item not found in cart.");
        }
        echo wp_json_encode($response);
    } else {
        wp_send_json_error('Nonce verification failed');
    }
    wp_die();
}

add_action('wp_ajax_dspp_remove_from_session', 'dspp_remove_from_session'); // For logged-in users
add_action('wp_ajax_nopriv_dspp_remove_from_session', 'dspp_remove_from_session'); // For non-logged-in users