<?php if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly 
/* phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery.SchemaChange */
?>
<?php 
    $kirb_page_ids = array();

    $page_ids_json = kirbGetMeta('_page_ids');

    if(!$page_ids_json) {

        $alternate_page_slugs = array(
            'loose-diamonds' => array('loose-diamonds','natural-diamonds','mined-diamonds'),
            'gemstone-diamonds' => array('gemstone-diamonds','natural-gemstones','gemstones'),
            'lab-diamonds' => array('lab-diamonds','lab-created-diamonds', 'lab-grown-diamonds'),
            'color-diamonds' => array('color-diamonds','color-lab-diamonds', 'fancy-color-diamonds'),
            'diamond-detail' => array('diamond-detail','detail-page', 'diamond-info'),
            'byor-review' => array('byor-review-page','build-your-own-ring','byor')
        );

        $final_slugs = array(
            'loose-diamonds' => $alternate_page_slugs['loose-diamonds'][0],
            'gemstone-diamonds' => $alternate_page_slugs['gemstone-diamonds'][0],
            'lab-diamonds' => $alternate_page_slugs['lab-diamonds'][0],
            'color-diamonds' => $alternate_page_slugs['color-diamonds'][0],
            'diamond-detail' => $alternate_page_slugs['diamond-detail'][0],
            'byor-review' => $alternate_page_slugs['byor-review'][0]
        );

        foreach($alternate_page_slugs as $page => $value) {
            foreach($value as $slug) {
                if(!kirb_get_post_id_by_slug($slug)) {
                    $final_slugs[$page] = $slug;
                    break;
                }
            }
        }

        $kirb_page_ids['loose-diamonds'] = kirb_create_pages('Loose Diamonds','[KIRB-RingBuilder-Listing category="NATURAL"]',$final_slugs['loose-diamonds'],true);
        $kirb_page_ids['gemstone-diamonds'] = kirb_create_pages('Gemstone','[KIRB-RingBuilder-Listing category="GEMSTONE"]',$final_slugs['gemstone-diamonds'],true);
        $kirb_page_ids['lab-diamonds'] = kirb_create_pages('Lab Created','[KIRB-RingBuilder-Listing category="LAB CREATED"]',$final_slugs['lab-diamonds'],true);
        $kirb_page_ids['color-diamonds'] = kirb_create_pages('Colored Diamonds','[KIRB-RingBuilder-Listing category="COLORED DIAMONDS"]',$final_slugs['color-diamonds'] ,true);
        $kirb_page_ids['diamond-detail'] = kirb_create_pages('Diamond Detail Page','[KIRB-RingBuilder-DetailPage]',$final_slugs['diamond-detail'] ,true);
        $kirb_page_ids['byor-review'] = kirb_create_pages('BYOR Review','[KIRB-RingBuilder-BYORPage]',$final_slugs['byor-review'],true);

        kirbAddMeta('_page_ids',wp_json_encode($kirb_page_ids));
    } else {
        global $wpdb;
        $page_ids_array = json_decode($page_ids_json, true);
        $page_ids = array_values($page_ids_array);

        $ids = array_map('absint', $page_ids);
        if (! empty($ids)) {
            $placeholders = implode(',', array_fill(0, count($ids), '%d'));
            $table = $wpdb->posts; // table name from $wpdb
            // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
            $wpdb->query($wpdb->prepare("UPDATE {$table} SET post_status = %s WHERE ID IN ($placeholders)", array_merge(array('publish'), $ids)));
        }
    }

    // Create default category
    if (!term_exists('kirb-diamonds', 'product_cat')) {
        wp_insert_term('Diamonds', 'product_cat', array(
            'slug' => 'kirb-diamonds'
        ));
    } else {
        $term = get_term_by('slug', 'kirb-diamonds', 'product_cat');
        wp_update_term($term->term_id, 'product_cat', array(
            'parent' => 0
        ));
    }    

    $categories = array(
        'kirb-lab-diamonds' => "Lab Diamonds", 
        'kirb-loose-diamonds' => "Loose Diamonds", 
        'kirb-gemstone-diamonds' => "Gemstones",
        'kirb-color-diamonds' => "Colored Diamonds"
    );

    // Create default sub categories
    foreach ($categories as $slug => $name) {
        if (!term_exists($slug,'product_cat')) {
            wp_insert_term($name, 'product_cat', [
                'slug' => $slug,
                'parent' => get_term_by('slug', 'kirb-diamonds', 'product_cat')->term_id
            ]);
        }else{
            $term = get_term_by('slug', $slug, 'product_cat');
            wp_update_term($term->term_id, 'product_cat', array(
                'parent' => get_term_by('slug', 'kirb-diamonds', 'product_cat')->term_id
            ));
        }
    }
    
    kirb_clear_cache();
    
    // Auto flush rewrite rules
    if (function_exists('flush_rewrite_rules')) {
        flush_rewrite_rules();
    }
    
    if($is_plugin_active) {
        $kirb_filters_data = kirb_get_filters_data();
        $token = kirbGetMeta("_store_token");
        if(isset($kirb_filters_data['StoreDetail']['store_token']) && !$token) {
            kirbAddMeta("_store_token", $kirb_filters_data['StoreDetail']['store_token']);
        }
    }
/* phpcs:enable */
