<?php

namespace App\Http\Controllers;
use App\Models\Cart;
use App\Models\Customer;
use Illuminate\Support\Facades\Log;
use Illuminate\Http\Request;

class CartController extends Controller
{
    public function addtocart(Request $request){
        if(!$request->customer_id){
            $validate   =   $request->validate([
                'product_id'             => 'required',
                'quantity'               => 'required',
            ]);
            $newCustomer = new Customer([
                'customer_name' => 'user',
            ]);
            $newCustomer->save();
            $customer_id                        = $newCustomer->customer_id;
            if($request->size_name === 'gown_size'){
                // $gst_product_price              = $request->product_price;
                // log::info($gst_product_price);
                // if($gst_product_price <= 999){
                //     $gst_percentage             = '5';
                //     log::info($gst_percentage);
                //     $gst_amount                 = $gst_product_price * ($gst_percentage / 100);
                //     log::info($gst_amount);

                // }else{
                //     $gst_percentage             = '12';
                //     log::info($gst_percentage);
                //     $gst_amount                 = $gst_product_price * ($gst_percentage / 100);
                //     log::info($gst_amount);
                // }
                $cartcreation = new Cart([
                    'product_id'                => $request->product_id,
                    'quantity'                  => $request->quantity,
                    'customer_id'               => $customer_id,
                    'silkchest'                 => $request->silkchest,
                    'Waist'                     => $request->Waist,
                    'ankle'                     => $request->ankle,
                    'age'                       => $request->age,
                    'quantity'                  => $request->quantity,
                    'product_actual_price'      => $request->product_actual_price,
                    'product_discount_price'    => $request->product_discount_price,
                    'other_customization_ids'   => $request->other_customization_ids,
                    // 'product_price'             => $request->product_price,
                    // 'gst_percentage'            => $gst_percentage,
                    // 'gst_value'                 => $gst_amount,
                    'subtotal'                  => $request->subtotal,
                    'size_name'                 => $request->size_name,
                    'variation_id'              => $request->variation_id,

                ]);
            }elseif($request->size_name === 'pavadai_sattai_size'){

                // $gst_product_price              = $request->product_price;
                // log::info($gst_product_price);
                // if($gst_product_price <= 999){
                //     $gst_percentage             = '5';
                //     log::info($gst_percentage);
                //     $gst_amount                 = $gst_product_price * ($gst_percentage / 100);
                // }else{
                //     $gst_percentage             = '12';
                //     log::info($gst_percentage);
                //     $gst_amount                 = $gst_product_price * ($gst_percentage / 100);
                //     log::info($gst_amount);
                // }
                $cartcreation = new Cart([
                    'product_id'                => $request->product_id,
                    'quantity'                  => $request->quantity,
                    'customer_id'               => $customer_id,
                    'chest'                     => $request->chest,
                    'blouse'                    => $request->blouse,
                    'skirt'                     => $request->skirt,
                    'age'                       => $request->age,
                    'quantity'                  => $request->quantity,
                    'product_actual_price'      => $request->product_actual_price,
                    'product_discount_price'    => $request->product_discount_price,
                    'other_customization_ids'   => $request->other_customization_ids,
                    // 'product_price'             => $request->product_price,
                    // 'gst_percentage'            => $gst_percentage,
                    // 'gst_value'                 => $gst_amount,
                    'subtotal'                  => $request->subtotal,    
                    'size_name'                 => $request->size_name,
                    'variation_id'              => $request->variation_id,

                ]);
            }else{
                return response()->json(['status' => false,'data' => 'Invalid Size Name'],200);
            }
            
            $cartcreation->save();
        }else{
            if($request->size_name === 'gown_size'){
                // $gst_product_price              = $request->product_price;
                // log::info($gst_product_price);
                // if($gst_product_price <= 999){
                //     $gst_percentage             = '5';
                //     log::info($gst_percentage);
                //     $gst_amount                 = $gst_product_price * ($gst_percentage / 100);
                // }else{
                //     $gst_percentage             = '12';
                //     log::info($gst_percentage);
                //     $gst_amount                 = $gst_product_price * ($gst_percentage / 100);
                //     log::info($gst_amount);
                // }
                $cartcreation = new Cart([
                    'product_id'                => $request->product_id,
                    'quantity'                  => $request->quantity,
                    'customer_id'               => $request->customer_id,
                    'silkchest'                 => $request->silkchest,
                    'Waist'                     => $request->Waist,
                    'ankle'                     => $request->ankle,
                    'age'                       => $request->age,
                    'quantity'                  => $request->quantity,
                    'product_actual_price'      => $request->product_actual_price,
                    'product_discount_price'    => $request->product_discount_price,
                    'other_customization_ids'   => $request->other_customization_ids,
                    // 'product_price'             => $request->product_price,
                    // 'gst_percentage'            => $gst_percentage,
                    // 'gst_value'                 => $gst_amount,
                    'subtotal'                  => $request->subtotal,
                    'size_name'                  => $request->size_name,
                    'variation_id'              => $request->variation_id,

                ]);
            }elseif($request->size_name === 'pavadai_sattai_size'){
                // $gst_product_price              = $request->product_price;
                // log::info($gst_product_price);
                // if($gst_product_price <= 999){
                //     $gst_percentage             = '5';
                //     log::info($gst_percentage);
                //     $gst_amount                 = $gst_product_price * ($gst_percentage / 100);
                //     log::info($gst_amount);
                // }else{
                //     $gst_percentage             = '12';
                //     log::info($gst_percentage);
                //     $gst_amount                 = $gst_product_price * ($gst_percentage / 100);
                //     log::info($gst_amount);
                // }
                $cartcreation = new Cart([
                    'product_id'                => $request->product_id,
                    'quantity'                  => $request->quantity,
                    'customer_id'               => $request->customer_id,
                    'chest'                     => $request->chest,
                    'blouse'                    => $request->blouse,
                    'skirt'                     => $request->skirt,
                    'age'                       => $request->age,
                    'quantity'                  => $request->quantity,
                    'product_actual_price'      => $request->product_actual_price,
                    'product_discount_price'    => $request->product_discount_price,
                    'other_customization_ids'   => $request->other_customization_ids,
                    // 'product_price'             => $request->product_price,
                    // 'gst_percentage'            => $gst_percentage,
                    // 'gst_value'                 => $gst_amount,
                    'subtotal'                  => $request->subtotal,    
                    'size_name'                 => $request->size_name,
                    'variation_id'              => $request->variation_id,

                ]);
            }else{
                return response()->json(['status' => false,'data' => 'Invalid Size Name'],200);
            } 
            $cartcreation->save();
            log::info($cartcreation);

        }
        if ($cartcreation){
            return response()->json(['success' => true,'message' => 'Add To Card Stored successfully', 'data' => $cartcreation],200);
        }else{
            return response()->json(['status' => false,'data' => 'failed Store'],200);
        }
    }

// get customer data
// $data  = Cart::where('customer_id', $customer_id)->with('productdetails','customerdetails')->get();

    public function customerCartDetailsget($customer_id){
        $data  = Cart::where('customer_id', $customer_id)->with('productdetails','customerdetails')->get();
        $productoveralltotal = 0;
        $fivepercentagevalue = 0;
        $twelvepercentagevalue = 0;

            // Exclusive
        // foreach ($data as $cartItem) {
        //     $productoveralltotal += $cartItem->subtotal;
        //     if ($cartItem->gst_percentage === '5') {
        //         $dataone  = Cart::where('customer_id', $customer_id)->where('gst_percentage', 5)->get();
        //         foreach ($dataone as $calculation) {
        //             $fivepercentagevalue += $calculation->gst_value;
        //         }
        //     } elseif ($cartItem->gst_percentage === '12') {
        //         $datatwo  = Cart::where('customer_id', $customer_id)->where('gst_percentage', 12)->get();
        //         foreach ($datatwo as $calculation) {
        //             $twelvepercentagevalue += $calculation->gst_value;
        //         }
        //     }
        // }
       
        foreach ($data as $item) {
            $item->othercustomization = $item->categorieslist();
            // $item->variation = $item->productvariationdetailses();
        }
        if ($data){
            return response()->json(['success' => true,'message' => 'Cart data get successfully', 'data' => $data,'productoveralltotal' => $productoveralltotal,],200);
        }else{
            return response()->json(['status' => false,'data' => 'failed get data'],200);
        }
    }
    // update cart data
    public function updateCartData(Request $request){
        $validate   =   $request->validate([
            'cart_id'            => 'required',
            'product_id'             => 'required',
            'quantity'               => 'required',
            'subtotal'               => 'required',
        ]);
        $data                  = Cart::find($request->cart_id);
        if (!$data) {
            return response()->json(['status' => false, 'message' => 'Cart item not found'], 404);
        }
        $data->quantity                      = $request->quantity;
        $data->subtotal                      = $request->subtotal;
        $data->product_actual_price          = $request->product_actual_price;
        $data->product_discount_price        = $request->product_discount_price;
        $updated = $data->save();
        if ($updated){
            return response()->json(['success' => true,'message' => 'Quantity Add successfully', 'data' => $data],200);
        }else{
            return response()->json(['status' => false,'data'    => 'Failed To Quantity Add'],200);
        }
    }

    ## STORE
    public function storecart(Request $request){
        $validate   =   $request->validate([
            'customer_id'            => 'required',
            'product_id'             => 'required',
            //'size_customization_id'  => 'required',
            'quantity'               => 'required',
            'subtotal'               => 'required',
        ]);
        $storedata                = new Cart();
        $storedata->customer_id   = $request->customer_id;
        $storedata->product_id    = $request->product_id;
        $storedata->quantity      = $request->quantity;
        $storedata->subtotal      = $request->subtotal;
        $store                    =  $storedata->save();
        if ($store == true){
            return response()->json(['success' => true,'data' => 'Cart data added Successfully'],200);
        }else{
            return response()->json(['status' => false,'data' => 'failed Store'],200);
        }
    }

    ## DESTORY
    public function destroycarts($id){
        $destroy     = Cart::where('cart_id', $id)->delete();
        if ($destroy == true) {
            return response()->json(['status' => true,'message'=>'destroy Success'],200);
        }else{
            return response()->json(['status' => false,'message'=>'failed destroy'],400);
        }
    }

    ## GET CART DETAILS
    public function getcarts(){
        $getdata = Cart::with('productdetails')
                        ->with('customerdetails')
                        ->get();
        if ($getdata->isNotEmpty()){
            return response()->json(['status' => true, 'Records' => $getdata], 200);
        } else {
            return response()->json(['status' => false,'data' => 'failed data'], 400);
        }
    }

    ## FIND CUSTOMER BASE 
    public function findcustomeridbase($id){
        $findCustomerBaseData = Cart::where('customer_id', $id)->with('productdetails')
                                                                ->with('customerdetails')
                                                                ->with('productimageDetails')
                                                                ->get();
        if ($findCustomerBaseData->isNotEmpty()){
            return response()->json(['status' => true, 'Records' => $findCustomerBaseData], 200);
        }else if($findCustomerBaseData->isEmpty()){
            return response()->json(['status' => true, 'data' => 'data is empty'], 200);
        } else {
            return response()->json(['status' => false,'data' => 'failed data'], 400);
        }
    }


}
