<?php

namespace App\Http\Controllers;



use Illuminate\Http\Request;

use App\Models\User;

use Illuminate\Support\Facades\View;

use App\Models\Diamond;
use App\Models\UserPage;
use App\Models\Shopify_theme_changes;
// use App\Http\Controllers\Auth;


class PageController extends Controller

{
    public function pageCreateDataChange(){

        $userId = 1 ; //Auth::user()->id;
        $data = Shopify_theme_changes::where('user_id', $userId)->get();
    
        $diamond_query = "select product_promotion ,available_type , is_build_product , is_superdeals , is_new , products_id , products_price , products_blurb , products_name , products_style_no , products_seo_url , design_id, diamond_quality_id, diamond_weight_group_id, dwg_name, dqg_alias, delivery_days, dcst_name, center_stone_type_id, (select pi.image_url from shop_products_image as pi where a.products_id = pi.products_id and pi.image_type = 'image' order by pi.sort_order asc limit 1) as image_url, 'image' as image_type , products_vendor_path, vender_id, media_path from ( (select pm.product_promotion ,pm.available_type , pm.is_build_product , pm.is_superdeals , pm.is_new , pm.products_vendor_path , pm.products_id , if(c_markup_type=1,`products_price3`+COALESCE(c_markup_value,0),`products_price3`+((`products_price3`*COALESCE(c_markup_value,0))/100)) as products_price , pd.products_blurb , pd.products_name , products_style_no , products_seo_url , pm.sort_order, pm.design_id, pm.diamond_quality_id, pm.diamond_weight_group_id, dwg.dwg_name as dwg_name, dqg.dqg_alias as dqg_alias, pm.delivery_days, css.dcst_name, pm.center_stone_type_id, vender_id, vm.media_path from `shop_products` as pm left join client_markup cmark on cmark.retailerid = 500 and products_price3 between cmark.price_from and cmark.price_to and cmark.type = '3' left join vendor_master vm on vm.vendorid = pm.vender_id left join diamond_weight_group as dwg on dwg.dwg_id = pm.diamond_weight_group_id left join diamond_quality_group as dqg on dqg.dqg_id = pm.diamond_quality_id left join diamond_center_stone_type as css on pm.center_stone_type_id = css.dcst_id left join shop_products_description as pd on pm.products_id = pd.products_id and language_id = '1' left join shop_product_to_product_group as ptpg on pm.products_id = ptpg.ptpg_products_id join shop_products_to_categories as ptc on pm.products_id = ptc.products_id left join shop_products_to_shape as pts on pm.products_id = pts.products_id left join shop_products_to_style_category as sptsc on pm.products_id = sptsc.sptsc_products_id left join shop_products_to_metal_type as sptmt on pm.products_id = sptmt.sptmt_products_id where 1 AND vm.vendor_status = 1 AND vm.deleted = 0 and product_image_status = 1 AND ( ptpg.ptpg_level = '1' ) AND products_quantity <> 0 AND ( ptc.categories_id = '6' OR ptc.categories_id = '74' OR ptc.categories_id = '89' OR ptc.categories_id = '4' OR ptc.categories_id = '85' OR ptc.categories_id = '3' OR ptc.categories_id = '90') AND products_status = 1 and ( ptc.categories_id = '1' or ptc.categories_id = '3' or ptc.categories_id = '4' or ptc.categories_id = '5' or ptc.categories_id = '6' or ptc.categories_id = '7' or ptc.categories_id = '8' or ptc.categories_id = '9' or ptc.categories_id = '10' or ptc.categories_id = '11' or ptc.categories_id = '67' or ptc.categories_id = '68' or ptc.categories_id = '69' or ptc.categories_id = '70' or ptc.categories_id = '71' or ptc.categories_id = '72' or ptc.categories_id = '73' or ptc.categories_id = '74' or ptc.categories_id = '77' or ptc.categories_id = '79' or ptc.categories_id = '83' or ptc.categories_id = '84' or ptc.categories_id = '85' or ptc.categories_id = '86' or ptc.categories_id = '87' or ptc.categories_id = '88' or ptc.categories_id = '89' or ptc.categories_id = '90' or ptc.categories_id = '91' ) AND ( pts.shape_id = '1' OR pts.shape_id = '2' OR pts.shape_id = '10' OR pts.shape_id = '6' OR pts.shape_id = '3' OR pts.shape_id = '5' OR pts.shape_id = '4' OR pts.shape_id = '7' OR pts.shape_id = '8' OR pts.shape_id = '9' OR pts.shape_id = '13' OR pts.shape_id = '12' OR pts.shape_id = '14' OR pts.shape_id = '15' OR pts.shape_id = '11') AND ( sptsc.sptsc_style_category_id = '34' OR sptsc.sptsc_style_category_id = '35' OR sptsc.sptsc_style_category_id = '70' OR sptsc.sptsc_style_category_id = '265' OR sptsc.sptsc_style_category_id = '168' OR sptsc.sptsc_style_category_id = '171' OR sptsc.sptsc_style_category_id = '172' OR sptsc.sptsc_style_category_id = '250' OR sptsc.sptsc_style_category_id = '252' OR sptsc.sptsc_style_category_id = '254' OR sptsc.sptsc_style_category_id = '255' OR sptsc.sptsc_style_category_id = '280' OR sptsc.sptsc_style_category_id = '902' OR sptsc.sptsc_style_category_id = '960' OR sptsc.sptsc_style_category_id = '961' OR sptsc.sptsc_style_category_id = '984' OR sptsc.sptsc_style_category_id = '985' OR sptsc.sptsc_style_category_id = '143' OR sptsc.sptsc_style_category_id = '145' OR sptsc.sptsc_style_category_id = '196' OR sptsc.sptsc_style_category_id = '147' OR sptsc.sptsc_style_category_id = '170' OR sptsc.sptsc_style_category_id = '224' OR sptsc.sptsc_style_category_id = '910' OR sptsc.sptsc_style_category_id = '946' OR sptsc.sptsc_style_category_id = '998' OR sptsc.sptsc_style_category_id = '990' OR sptsc.sptsc_style_category_id = '999' OR sptsc.sptsc_style_category_id = '1000' OR sptsc.sptsc_style_category_id = '1001' OR sptsc.sptsc_style_category_id = '1002' OR sptsc.sptsc_style_category_id = '1003' OR sptsc.sptsc_style_category_id = '962' OR sptsc.sptsc_style_category_id = '979' OR sptsc.sptsc_style_category_id = '981' OR sptsc.sptsc_style_category_id = '73' OR sptsc.sptsc_style_category_id = '174' OR sptsc.sptsc_style_category_id = '983' OR sptsc.sptsc_style_category_id = '218' OR sptsc.sptsc_style_category_id = '266' OR sptsc.sptsc_style_category_id = '241' OR sptsc.sptsc_style_category_id = '243' OR sptsc.sptsc_style_category_id = '245' OR sptsc.sptsc_style_category_id = '247' OR sptsc.sptsc_style_category_id = '276' OR sptsc.sptsc_style_category_id = '278' OR sptsc.sptsc_style_category_id = '279' OR sptsc.sptsc_style_category_id = '896' OR sptsc.sptsc_style_category_id = '914' OR sptsc.sptsc_style_category_id = '86' OR sptsc.sptsc_style_category_id = '88' OR sptsc.sptsc_style_category_id = '91' OR sptsc.sptsc_style_category_id = '952' OR sptsc.sptsc_style_category_id = '958' OR sptsc.sptsc_style_category_id = '959' OR sptsc.sptsc_style_category_id = '963' OR sptsc.sptsc_style_category_id = '964' OR sptsc.sptsc_style_category_id = '965' OR sptsc.sptsc_style_category_id = '966' OR sptsc.sptsc_style_category_id = '967' OR sptsc.sptsc_style_category_id = '968' OR sptsc.sptsc_style_category_id = '969' OR sptsc.sptsc_style_category_id = '970' OR sptsc.sptsc_style_category_id = '972' OR sptsc.sptsc_style_category_id = '974') AND ( sptmt.sptmt_metal_type_id = '10') AND ( sptmt.sptmt_metal_type_id = '4' OR sptmt.sptmt_metal_type_id = '8' OR sptmt.sptmt_metal_type_id = '10' OR sptmt.sptmt_metal_type_id = '11' OR sptmt.sptmt_metal_type_id = '12' OR sptmt.sptmt_metal_type_id = '13' OR sptmt.sptmt_metal_type_id = '14') AND ( pm.diamond_quality_id = '1' OR pm.diamond_quality_id = '62' OR pm.diamond_quality_id = '3' OR pm.diamond_quality_id = '57' OR pm.diamond_quality_id = '6' OR pm.diamond_quality_id = '63' OR pm.diamond_quality_id = '9' OR pm.diamond_quality_id = '64' OR pm.diamond_quality_id = '12' OR pm.diamond_quality_id = '58' OR pm.diamond_quality_id = '13' OR pm.diamond_quality_id = '14' OR pm.diamond_quality_id = '52' OR pm.diamond_quality_id = '54' OR pm.diamond_quality_id = '53' OR pm.diamond_quality_id = '55' OR pm.diamond_quality_id = '17' OR pm.diamond_quality_id = '50' OR pm.diamond_quality_id = '51' OR pm.diamond_quality_id = '44' OR pm.diamond_quality_id = '45' OR pm.diamond_quality_id = '19' OR pm.diamond_quality_id = '21' OR pm.diamond_quality_id = '22' OR pm.diamond_quality_id = '23' OR pm.diamond_quality_id = '46' OR pm.diamond_quality_id = '49' OR pm.diamond_quality_id = '41' OR pm.diamond_quality_id = '40' OR pm.diamond_quality_id = '42' OR pm.diamond_quality_id = '43' OR pm.diamond_quality_id = '47' OR pm.diamond_quality_id = '48' OR pm.diamond_quality_id = '66' OR pm.diamond_quality_id = '59' OR pm.diamond_quality_id = '60' OR pm.diamond_quality_id = '65' OR pm.diamond_quality_id = '61' OR pm.diamond_quality_id = '56') AND pm.center_stone_type_id = '3' AND ( pm.vender_id = '1' OR pm.vender_id = '6' OR pm.vender_id = '16') group by pm.design_id ORDER BY sort_order ASC ) union all (select pm.product_promotion ,pm.available_type , pm.is_build_product , pm.is_superdeals , pm.is_new , pm.products_vendor_path , pm.products_id , if(c_markup_type=1,`products_price3`+COALESCE(c_markup_value,0),`products_price3`+((`products_price3`*COALESCE(c_markup_value,0))/100)) as products_price , pd.products_blurb , pd.products_name , products_style_no , products_seo_url , pm.sort_order, pm.design_id, pm.diamond_quality_id, pm.diamond_weight_group_id, dwg.dwg_name as dwg_name, dqg.dqg_alias as dqg_alias, pm.delivery_days, css.dcst_name, pm.center_stone_type_id, vender_id, vm.media_path from `shop_products` as pm left join client_markup cmark on cmark.retailerid = 500 and products_price3 between cmark.price_from and cmark.price_to and cmark.type = '3' left join vendor_master vm on vm.vendorid = pm.vender_id left join diamond_weight_group as dwg on dwg.dwg_id = pm.diamond_weight_group_id left join diamond_quality_group as dqg on dqg.dqg_id = pm.diamond_quality_id left join diamond_center_stone_type as css on pm.center_stone_type_id = css.dcst_id left join shop_products_description as pd on pm.products_id = pd.products_id and language_id = '1' left join shop_product_to_product_group as ptpg on pm.products_id = ptpg.ptpg_products_id join shop_products_to_categories as ptc on pm.products_id = ptc.products_id left join shop_products_to_shape as pts on pm.products_id = pts.products_id left join shop_products_to_style_category as sptsc on pm.products_id = sptsc.sptsc_products_id left join shop_products_to_metal_type as sptmt on pm.products_id = sptmt.sptmt_products_id where 1 AND vm.vendor_status = 1 AND vm.deleted = 0 and product_image_status = 1 AND ( ptpg.ptpg_level = '1' ) AND products_quantity <> 0 AND ( ptc.categories_id = '6' OR ptc.categories_id = '74' OR ptc.categories_id = '89' OR ptc.categories_id = '4' OR ptc.categories_id = '85' OR ptc.categories_id = '3' OR ptc.categories_id = '90') AND products_status = 1 and ( ptc.categories_id = '1' or ptc.categories_id = '3' or ptc.categories_id = '4' or ptc.categories_id = '5' or ptc.categories_id = '6' or ptc.categories_id = '7' or ptc.categories_id = '8' or ptc.categories_id = '9' or ptc.categories_id = '10' or ptc.categories_id = '11' or ptc.categories_id = '67' or ptc.categories_id = '68' or ptc.categories_id = '69' or ptc.categories_id = '70' or ptc.categories_id = '71' or ptc.categories_id = '72' or ptc.categories_id = '73' or ptc.categories_id = '74' or ptc.categories_id = '77' or ptc.categories_id = '79' or ptc.categories_id = '83' or ptc.categories_id = '84' or ptc.categories_id = '85' or ptc.categories_id = '86' or ptc.categories_id = '87' or ptc.categories_id = '88' or ptc.categories_id = '89' or ptc.categories_id = '90' or ptc.categories_id = '91' ) AND ( pts.shape_id = '1' OR pts.shape_id = '2' OR pts.shape_id = '10' OR pts.shape_id = '6' OR pts.shape_id = '3' OR pts.shape_id = '5' OR pts.shape_id = '4' OR pts.shape_id = '7' OR pts.shape_id = '8' OR pts.shape_id = '9' OR pts.shape_id = '13' OR pts.shape_id = '12' OR pts.shape_id = '14' OR pts.shape_id = '15' OR pts.shape_id = '11') AND ( sptsc.sptsc_style_category_id = '34' OR sptsc.sptsc_style_category_id = '35' OR sptsc.sptsc_style_category_id = '70' OR sptsc.sptsc_style_category_id = '265' OR sptsc.sptsc_style_category_id = '168' OR sptsc.sptsc_style_category_id = '171' OR sptsc.sptsc_style_category_id = '172' OR sptsc.sptsc_style_category_id = '250' OR sptsc.sptsc_style_category_id = '252' OR sptsc.sptsc_style_category_id = '254' OR sptsc.sptsc_style_category_id = '255' OR sptsc.sptsc_style_category_id = '280' OR sptsc.sptsc_style_category_id = '902' OR sptsc.sptsc_style_category_id = '960' OR sptsc.sptsc_style_category_id = '961' OR sptsc.sptsc_style_category_id = '984' OR sptsc.sptsc_style_category_id = '985' OR sptsc.sptsc_style_category_id = '143' OR sptsc.sptsc_style_category_id = '145' OR sptsc.sptsc_style_category_id = '196' OR sptsc.sptsc_style_category_id = '147' OR sptsc.sptsc_style_category_id = '170' OR sptsc.sptsc_style_category_id = '224' OR sptsc.sptsc_style_category_id = '910' OR sptsc.sptsc_style_category_id = '946' OR sptsc.sptsc_style_category_id = '998' OR sptsc.sptsc_style_category_id = '990' OR sptsc.sptsc_style_category_id = '999' OR sptsc.sptsc_style_category_id = '1000' OR sptsc.sptsc_style_category_id = '1001' OR sptsc.sptsc_style_category_id = '1002' OR sptsc.sptsc_style_category_id = '1003' OR sptsc.sptsc_style_category_id = '962' OR sptsc.sptsc_style_category_id = '979' OR sptsc.sptsc_style_category_id = '981' OR sptsc.sptsc_style_category_id = '73' OR sptsc.sptsc_style_category_id = '174' OR sptsc.sptsc_style_category_id = '983' OR sptsc.sptsc_style_category_id = '218' OR sptsc.sptsc_style_category_id = '266' OR sptsc.sptsc_style_category_id = '241' OR sptsc.sptsc_style_category_id = '243' OR sptsc.sptsc_style_category_id = '245' OR sptsc.sptsc_style_category_id = '247' OR sptsc.sptsc_style_category_id = '276' OR sptsc.sptsc_style_category_id = '278' OR sptsc.sptsc_style_category_id = '279' OR sptsc.sptsc_style_category_id = '896' OR sptsc.sptsc_style_category_id = '914' OR sptsc.sptsc_style_category_id = '86' OR sptsc.sptsc_style_category_id = '88' OR sptsc.sptsc_style_category_id = '91' OR sptsc.sptsc_style_category_id = '952' OR sptsc.sptsc_style_category_id = '958' OR sptsc.sptsc_style_category_id = '959' OR sptsc.sptsc_style_category_id = '963' OR sptsc.sptsc_style_category_id = '964' OR sptsc.sptsc_style_category_id = '965' OR sptsc.sptsc_style_category_id = '966' OR sptsc.sptsc_style_category_id = '967' OR sptsc.sptsc_style_category_id = '968' OR sptsc.sptsc_style_category_id = '969' OR sptsc.sptsc_style_category_id = '970' OR sptsc.sptsc_style_category_id = '972' OR sptsc.sptsc_style_category_id = '974') AND ( sptmt.sptmt_metal_type_id = '10') AND ( sptmt.sptmt_metal_type_id = '4' OR sptmt.sptmt_metal_type_id = '8' OR sptmt.sptmt_metal_type_id = '10' OR sptmt.sptmt_metal_type_id = '11' OR sptmt.sptmt_metal_type_id = '12' OR sptmt.sptmt_metal_type_id = '13' OR sptmt.sptmt_metal_type_id = '14') AND ( pm.diamond_quality_id = '1' OR pm.diamond_quality_id = '62' OR pm.diamond_quality_id = '3' OR pm.diamond_quality_id = '57' OR pm.diamond_quality_id = '6' OR pm.diamond_quality_id = '63' OR pm.diamond_quality_id = '9' OR pm.diamond_quality_id = '64' OR pm.diamond_quality_id = '12' OR pm.diamond_quality_id = '58' OR pm.diamond_quality_id = '13' OR pm.diamond_quality_id = '14' OR pm.diamond_quality_id = '52' OR pm.diamond_quality_id = '54' OR pm.diamond_quality_id = '53' OR pm.diamond_quality_id = '55' OR pm.diamond_quality_id = '17' OR pm.diamond_quality_id = '50' OR pm.diamond_quality_id = '51' OR pm.diamond_quality_id = '44' OR pm.diamond_quality_id = '45' OR pm.diamond_quality_id = '19' OR pm.diamond_quality_id = '21' OR pm.diamond_quality_id = '22' OR pm.diamond_quality_id = '23' OR pm.diamond_quality_id = '46' OR pm.diamond_quality_id = '49' OR pm.diamond_quality_id = '41' OR pm.diamond_quality_id = '40' OR pm.diamond_quality_id = '42' OR pm.diamond_quality_id = '43' OR pm.diamond_quality_id = '47' OR pm.diamond_quality_id = '48' OR pm.diamond_quality_id = '66' OR pm.diamond_quality_id = '59' OR pm.diamond_quality_id = '60' OR pm.diamond_quality_id = '65' OR pm.diamond_quality_id = '61' OR pm.diamond_quality_id = '56') AND pm.center_stone_type_id = '3' AND ( pm.vender_id = '1' OR pm.vender_id = '6' OR pm.vender_id = '16') and diamond_weight_group_id <> '' and diamond_weight_group_id <> 0 and diamond_quality_id <> '' and diamond_quality_id <> 0 group by pm.design_id ORDER BY sort_order ASC ) )as a group by products_id ORDER BY sort_order ASC LIMIT 0 , 150";
        $records = DB::connection('mysql2')->select($diamond_query); 
        // print_r($records);
        return view('welcome', compact('data','records'));

    }


    public function createPage(Request $request)
    {
        
     $userId = $request->user_id; 
     $retaillerid = $request->retaillerid; 
     $text_color = $request->text_color; 
     $border_color = $request->border_color; 
     $button_background = $request->button_background; 
     $button_text_color = $request->button_text_color; 
     $button_background_hover = $request->button_background_hover; 
     $button_hover_color = $request->button_hover_color; 
     $input_bg = $request->input_bg;

     $body_color = $request->body_color;
     $heading_color = $request->heading_color;
     $heading_font_size = $request->heading_font_size;
     $primary_font_size = $request->primary_font_size;
     $filter_font_size = $request->filter_font_size;
     $sub_heading_font_size = $request->sub_heading_font_size;
     $primary_link_color = $request->primary_link_color;
     


     $user = User::find($userId);

     $bodyHtml = View::make('jewelry-page')->render();
     $bodyHtml2 = View::make('jewelry-detalis')->render();
          $data = [ 'page' => [
        
                        'title' => 'jewelry Product',
                        'body_html' => $bodyHtml,
                         ]
                 ];
          $data2 = [ 'page' => [
    
                    'title' => 'jewelry Detalis',
                    'body_html' => $bodyHtml2,
                     ]
             ];
            $userPage_uid = UserPage::where('user_id', $userId)->first();
            //mnue
            //  $menuData = [
            //     'menu' => [
            //         'title' => 'Main Menu',
            //         'links' => [
            //             [
            //                 'title' => 'Diamond Product',
            //                 'url' => '/pages/diamond-product',
            //                 'position' => 3,
            //             ]
            //         ]
            //     ]
            // ];
            //  $response = $user->api()->rest('POST', '/admin/api/2024-01/menus.json', $menuData);
            // dd($userPage_uid->diamond_Detalis_id);
            if(empty($userPage_uid)){
                 $response = $user->api()->rest('POST', '/admin/api/2024-01/pages.json', $data);
                 $response2 = $user->api()->rest('POST', '/admin/api/2024-01/pages.json',  $data2);
                 //  dd($response['body']['container']['page']['id']);
                if($response['body']['container']['page']['id'] && $response2['body']['container']['page']['id']) {
                  
                    $pageId = $response['body']['container']['page']['id'];
                    $pageId2 = $response2['body']['container']['page']['id'];
            
                    UserPage::create([
                        'user_id' => $userId,
                        'page_id' => $pageId,  // Save the Shopify page ID
                        'diamond_Detalis_id' => $pageId2,
                    ]);

                
     
                    Shopify_theme_changes::create([
                        'user_id' => $userId,
                        'retaillerid' => $retaillerid,
                        'text_color' => $text_color,
                        'border_color' => $border_color,
                        'button_background' => $button_background,  
                        'button_text_color' => $button_text_color,  
                        'button_background_hover' => $button_background_hover, 
                        'button_hover_color' => $button_hover_color,
                        'input_bg' => $input_bg,
                        'body_color' => $body_color,
                        'heading_color' => $heading_color,
                        'heading_font_size' => $heading_font_size,
                        'primary_font_size' => $primary_font_size,
                        'primary_link_color' => $primary_link_color,
                        'filter_font_size' => $filter_font_size,
                        'sub_heading_font_size' => $sub_heading_font_size,

                    ]);
            
                    // Return a success response
                    return response()->json('Page created successfully.');
                } else {
                    return response()->json([
                        'error' => 'Failed to create page.',
                        'details' => $response,
                    ], 400);
                }
               }else{
                  $dataco =  Shopify_theme_changes::where('user_id', $userId)
                   ->update([
                       'retaillerid' => $retaillerid,
                        'text_color' => $text_color,
                        'border_color' => $border_color,
                        'button_background' => $button_background,  
                        'button_text_color' => $button_text_color,  
                        'button_background_hover' => $button_background_hover, 
                        'button_hover_color' => $button_hover_color,
                        'input_bg' => $input_bg,
                        'body_color' => $body_color,
                        'heading_color' => $heading_color,
                        'heading_font_size' => $heading_font_size,
                        'primary_font_size' => $primary_font_size,
                        'primary_link_color' => $primary_link_color,
                        'filter_font_size' => $filter_font_size,
                        'sub_heading_font_size' => $sub_heading_font_size,
                    ]);
                 

                   $response = $user->api()->rest('PUT', "/admin/api/2024-01/pages/".$userPage_uid->page_id.".json", $data);
                   $response2 = $user->api()->rest('PUT', "/admin/api/2024-01/pages/".$userPage_uid->diamond_Detalis_id.".json", $data2);

                  // Return full data in response
                    return response()->json([
                        'message' => 'Page Update successfully.',
                        'theme_update_status' => $dataco,
                       'request_data' => [
                                'retaillerid' => $retaillerid,
                                'text_color' => $text_color,
                                'border_color' => $border_color,
                                'button_background' => $button_background,  
                                'button_text_color' => $button_text_color,  
                                'button_background_hover' => $button_background_hover, 
                                'button_hover_color' => $button_hover_color,
                                'input_bg' => $input_bg,
                                'body_color' => $body_color,
                                'heading_color' => $heading_color,
                                'heading_font_size' => $heading_font_size,
                                'primary_font_size' => $primary_font_size,
                                'primary_link_color' => $primary_link_color,
                                'filter_font_size' => $filter_font_size,
                                'sub_heading_font_size' => $sub_heading_font_size,
                            ]
                    ]);
               
                   // return response()->json('Page Update successfully.');
               }     
        
    }

   public function shopify_theme_changes(Request $request)
    {
       
        $shop_name = $request->shop_name; 

        $user_id = User::where('name', $shop_name)->value('id');
        $data = Shopify_theme_changes::where('user_id', $user_id)->first();
        
        return response()->json([
            'success' => true,
            'data' => $data,
        ]);
        
    }


    public function deletePage(Request $request)
    {
        //dd($request);
        $userId = $request->user_id;
        $user = User::find($userId);
    
        // Fetch the user's page data from the database
        $userPage = UserPage::where('user_id', $userId)->first();
    
        // Check if the page data exists for the user
        if ($userPage) {
            // Delete the diamond product page
            $response = $user->api()->rest('DELETE', "/admin/api/2024-01/pages/{$userPage->page_id}.json");
    
            // Check for the success of the first page deletion
            if ($response['status'] == 200) {
                // If the diamond details page exists, delete it too
                if ($userPage->diamond_Detalis_id) {
                    $response2 = $user->api()->rest('DELETE', "/admin/api/2024-01/pages/{$userPage->diamond_Detalis_id}.json");
    
                    if ($response2['status'] == 200) {
                        // Optionally, remove the reference in your local database
                        $userPage->delete();
    
                        return response()->json('Pages deleted successfully.');
                    } else {
                        return response()->json([
                            'error' => 'Failed to delete diamond details page.',
                            'details' => $response2,
                        ], 400);
                    }
                } else {
                    return response()->json('Diamond details page does not exist.', 400);
                }
            } else {
                return response()->json([
                    'error' => 'Failed to delete diamond product page.',
                    'details' => $response,
                ], 400);
            }
        } else {
            return response()->json('No pages found for this user.', 404);
        }
    }


    public function getDiamonds()
    {
        // die('hello');
        $diamonds = Diamond::paginate(20);
        // $diamonds = Diamond::all(); // Fetch all diamonds
        // return response()->json($diamonds); // Return data as JSON

        return response()->json($diamonds)
        ->header('Access-Control-Allow-Origin', '*') // Allow any origin
        ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS')
        ->header('Access-Control-Allow-Headers', 'Content-Type, X-Auth-Token, Authorization, Origin');
    }

}



