<?php
if (!defined('ABSPATH')) {exit;}
/* phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery.SchemaChange */

global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$krfs_metadata = $wpdb->prefix . 'krfs_metadata';

$krfs_metadata_table = "CREATE TABLE IF NOT EXISTS $krfs_metadata (
    id INT(11) NOT NULL AUTO_INCREMENT,
    meta_key TEXT NOT NULL,
    meta_value TEXT NOT NULL,
    PRIMARY KEY (id)
) $charset_collate;";

require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($krfs_metadata_table);

if(!krfsapp_getMeta('_is_plugin_active')) {
    krfsapp_addMeta('_setting_primary_color', '#7646D7');
    krfsapp_addMeta('_setting_accent_color', '#EBE2FB');
    krfsapp_addMeta('_setting_primary_color_filter', 'invert(41%) sepia(21%) saturate(6164%) hue-rotate(237deg) brightness(89%) contrast(93%)');
    krfsapp_addMeta('_setting_products_per_page', 16);
    krfsapp_addMeta('_setting_cache_enabled', 'true');
    krfsapp_addMeta('_setting_catg_control', wp_json_encode(array(
        "Engagement Rings" => true,
        "Wedding Men Rings" => true,
        "Wedding Women Rings" => true,
        "Wedding Rings" => true,

    )));
    krfsapp_addMeta("_setting_ring_setting_catg", "");
    // $is_plugin_active = krfsapp_getMeta('_is_plugin_active') == 'true';
    // if ($is_plugin_active) {
    //     include_once("plugin_activation_hook.php");
    // }

    $page_ids = array();
    $page_ids['engagement-rings'] = krfsapp_create_pages('Engagement Rings', '[KRFS-EngagementBuilder-Listing category="engagement-rings"]', 'engagement-rings', true);
    $page_ids['wedding-men-rings'] = krfsapp_create_pages('Wedding Rings', '[KRFS-EngagementBuilder-Listing category="wedding-men-rings"]', 'wedding-men-rings', true);
    $page_ids['wedding-women-rings'] = krfsapp_create_pages('Wedding Rings', '[KRFS-EngagementBuilder-Listing category="wedding-women-rings"]', 'wedding-women-rings', true);
    $page_ids['wedding-rings'] = krfsapp_create_pages('Wedding Rings', '[KRFS-EngagementBuilder-Listing category="wedding-men-rings"]', 'wedding-men-rings', true);

    $categories = array(
        'engagement-rings' => "Engagement Ring", 
        'wedding-men-rings' => "Wedding Men Rings", 
        'wedding-women-rings' => "Wedding Women Rings",
        'wedding-rings' => "Wedding Rings"
    );

    // Create default sub categories
    foreach ($categories as $slug => $name) {
        if (!term_exists($slug,'product_cat')) {
            wp_insert_term($name, 'product_cat', [
                'slug' => $slug,
            ]);
        }
    }



    krfsapp_addMeta('_page_ids', wp_json_encode($page_ids));
    krfsapp_create_custom_product_attribute(
        'Shape', // Attribute name
        'shape_attr', // Attribute slug
        array('Round', 'Princess', 'Cushion', 'Emerald', 'Marquise', 'Oval', 'Radiant', 'Pear', 'Asscher', 'Heart') // Terms
    );

    krfsapp_create_custom_product_attribute(
        'Stone', // Attribute name
        'stone_attr', // Attribute slug
        array('Natural','Lab', 'Ruby', 'Emerald','BlueSapphire','PinkSapphire', 'amethyst', 'Morganite', 'BlackDiamond', 'YellowSapphire', 'Citrine')
    );

    // Attribute 2
    krfsapp_create_custom_product_attribute(
        'Style', // Attribute name
        'style_attr', // Attribute slug
        array('Solitaire','Halo','Three-Stone','Pave','Vintage','Bypass', 'Fancy', 'Multi-Row','Trellis', 'Single-Row', 'Side Stone') // Terms
    );

    // Attribute 3
    krfsapp_create_custom_product_attribute(
        'Metal', // Attribute name
        'metal_attr', // Attribute slug
        array('White Gold 14K', 'White Gold 18K', 'Yellow Gold 14K','Yellow Gold 18K', 
        'Rose Gold 14K', 'Rose Gold 18K', 'Platinum') // Terms
    );

    krfsapp_create_custom_product_attribute(
        'WedMen', // Attribute name
        'wed_men_attr', // Attribute slug
        array('Classic', 'Carved', 'Tantalum', 'Matte', 'Hammered','Carbon Fiber') // Terms
    );
    krfsapp_create_custom_product_attribute(
        'SideStone', // Attribute name
        'side_stone_attr', // Attribute slug
        array('Lab', 'Morganite', 'Natural', 'Amethyst', 'Ruby','Black diamond','Emerald','Yellow sapphire','Blue sapphire','Citrine','Pink sapphire') // Terms
    );
    krfsapp_create_custom_product_attribute(
        'WedWomen', // Attribute name
        'wed_women_attr', // Attribute slug
        array('Open', 'Unique', 'Modern', 'Vintage', 'Curved','Contoured Match','Nature','Stackable','Luxe','Plain Meta','Classic','Eternity') // Terms
    );
    krfsapp_create_custom_product_attribute(
        'MiscAttr', // Attribute name
        'miscs_attr', // Attribute slug
        array('bridal_sets','home_preview') // Terms
    );

    krfsapp_addMeta('_setting_section_controls', wp_json_encode(array(
        "eng_ring_styles" => true,
        "eng_ring_stones" => true,
        "wed_men_ring_styles" => true,
        "wed_women_ring_styles" => true,
        "metal" => true,
        "diamond_shapes" => true,
        "bridal_sets" => true,
        "home_preview" => true,
        "engraving" => false,
        "wed_ring_sizer" => false,
    )));

    krfsapp_addMeta('_setting_ring_wedding_catg','[]');
    krfsapp_addMeta('_setting_engraving_catg','[]');

    $krfs_default_mapping_data = [
        "engagement_ring_style" => "krfs_style_attr",
        "engagement_ring_style_terms" => [
            "solitaire"   => "solitaire",
            "halo"        => "halo",
            "three-stone" => "three-stone",
            "pave"        => "pave",
            "vintage"     => "vintage",
            "bypass"      => "bypass",
            "fancy"       => "fancy",
            "multi-row"   => "multi-row",
            "trellis"     => "trellis",
            "single-row"  => "single-row",
            "side_stone"  => "side_stone"
        ],
        "engagement_ring_sidestone" => "krfs_stone_attr",
        "engagement_ring_sidestone_terms" => [
            "lab"            => "lab",
            "morganite"      => "morganite",
            "natural"        => "natural",
            "amethyst"       => "amethyst",
            "ruby"           => "ruby",
            "black_diamond"  => "blackdiamond",
            "emerald"        => "emerald",
            "yellow_sapphire"=> "yellowsapphire",
            "blue_sapphire"  => "bluesapphire",
            "citrine"        => "citrine",
            "pink_sapphire"  => "pinksapphire"
        ],
        "wedding_ring_wed_men" => "krfs_wed_men_attr",
        "wedding_ring_terms_wedmen" => [
            "classic"       => "classic",
            "carved"        => "carved",
            "tantalum"      => "tantalum",
            "matte"         => "matte",
            "hammered"      => "hammered",
            "carbon_fiber"  => "carbon_fiber"
        ],
        "wedding_ring_wed_women" => "krfs_wed_women_attr",
        "wedding_ring_terms_wedwomen" => [
            "open"             => "open",
            "unique"           => "unique",
            "modern"           => "modern",
            "vintage"          => "vintage",
            "curved"           => "curved",
            "contoured_match"  => "contoured_match",
            "nature"           => "nature",
            "stackable"        => "stackable",
            "luxe"             => "luxe",
            "plain_meta"       => "plain_meta",
            "classic"          => "classic",
            "eternity"         => "eternity"
        ],
        "metal" => "krfs_metal_attr",
        "metal_terms" => [
            "white_gold_14k" => "white_gold_14k",
            "white_gold_18k" => "white_gold_18k",
            "yellow_gold_14k"=> "yellow_gold_14k",
            "yellow_gold_18k"=> "yellow_gold_18k",
            "rose_gold_14k"  => "rose_gold_14k",
            "rose_gold_18k"  => "rose_gold_18k",
            "platinum"       => "platinum"
        ],
        "shape" => "krfs_shape_attr",
        "shape_terms" => [
            "round"    => "round",
            "princess" => "princess",
            "cushion"  => "cushion",
            "emerald"  => "emerald",
            "marquise" => "marquise",
            "oval"     => "oval",
            "radiant"  => "radiant",
            "pear"     => "pear",
            "asscher"  => "asscher",
            "heart"    => "heart"
        ]
    ];

    if(!krfsapp_getMeta('attributes_mapping')) {
        krfsapp_addMeta('attributes_mapping', wp_json_encode($krfs_default_mapping_data));
    }
}



/* phpcs:enable */
