<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use App\Models\Order;
use App\Models\Cart;
use App\Models\Shipping_Address;
use Illuminate\Support\Facades\Log;
use App\Mail\OrderWebSubmittedMail;
use App\Mail\OrderConfirmedMail;
use App\Mail\OrderProcessingMail;
use App\Mail\OrderShippedMail;
use App\Mail\OrderDeliveredMail;
use App\Mail\OrderCanceledMail;







class MailController extends Controller
{
    
    // Web Order Submitted mail
    public function orderSuccessSubmittedMail(Request $request)         // Web Mail Order Success Mail

    {
        $fromEmail                              = 'karthichronoinfotech@gmail.com';
        $toEmail                                = $request->input('toemails');
        $ccEmails                               = $request->input('ccEmail');
        $order_id                               = $request->input('order_id');
        $orders                                 = Order::where('order_id', $order_id)->with('shippingaddressdetails')->first();
        $order_description                      = $orders->order_description;
        $delivery_charge                        = '100.00';
        $over_all_total                         = $orders->over_all_total;
        $order_status                           = $orders->order_status;
        $shipping_address_id                    = $orders->shipping_address_id;
        $cartIds                                = array_map('trim', explode(',', $orders->cart_id));
        // $shipping_address_id = json_decode($orders->shipping_address_id, true);
        $cartDetails                            = Cart::whereIn('cart_id', $cartIds)->with('productdetails')->get();
        $deliveryDetails                        = Shipping_Address::where('shipping_address_id',$shipping_address_id)->get();
        $customer_name                          = $deliveryDetails[0]->first_name . ' ' . $deliveryDetails[0]->last_name;
        $total_amount                           = $orders->subtotal;
        $promocode_value                        = $orders->promocode_value;

        if ($cartDetails) {
            $extractedCardDetails               = [];
            foreach ($cartDetails as $cartDetail) {
                $productDetails                 = [];
                $cartProductDetails             = $cartDetail->productdetails;
                foreach ($cartProductDetails as $product) {
                    $productDetails[] = [
                        'product_id'            => $product->product_id,
                        'product_name'          => $product->product_name,
                        'product_actual_price'  => $product->product_actual_price,
                        'product_discount_price'=> $product->product_discount_price,
                    ];
                } 
    
                $extractedCardDetails[] = [
                    'product_actual_price'      => $cartDetail->product_actual_price,
                    'product_discount_price'    => $cartDetail->product_discount_price,
                    'quantity'                  => $cartDetail->quantity,
                    'size_name'                 => $cartDetail->size_name,
                    'chest'                     => $cartDetail->chest,
                    'blouse'                    => $cartDetail->blouse,
                    'skirt'                     => $cartDetail->skirt,
                    'age'                       => $cartDetail->age,
                    'silkchest'                 => $cartDetail->silkchest,
                    'Waist'                     => $cartDetail->Waist,
                    'ankle'                     => $cartDetail->ankle,
                    'productdetailsemail'       => $productDetails,
                ];

            }
            log::info($extractedCardDetails);
    
    
        } else {
            log::error('Cart details are null for order ID: ' . $order_id);
        }
        // Send the email to multiple recipients
        try {
            Mail::to($toEmail)->send(new OrderWebSubmittedMail(
                $fromEmail, $toEmail, $ccEmails, $order_id, $customer_name, $extractedCardDetails, $order_status, $delivery_charge, $over_all_total, $deliveryDetails, $order_description,$total_amount,$promocode_value
            ));
            return response()->json(['status' => true,'message'=>'Email sent successfully' ],200);
        } catch (\Exception $e) {
            return response()->json(['status' => false, 'message' => 'Error sending email.', 'data' => $e->getMessage()]);
        }
    }

    // Web Order Confirmed mail
    public function orderConfirmedMail(Request $request)         // Web Mail Order Success Mail
    {
        $fromEmail                              = 'karthichronoinfotech@gmail.com';
        $toEmail                                = $request->input('toemails');
        $ccEmails                               = $request->input('ccEmail');
        $order_id                               = $request->input('order_id');
        $orders                                 = Order::where('order_id', $order_id)->with('shippingaddressdetails')->first();
        $order_description                      = $orders->order_description;
        $delivery_charge                        = '100.00';
        $over_all_total                         = $orders->over_all_total;
        $order_status                           = $orders->order_status;
        $shipping_address_id                    = $orders->shipping_address_id;
        $cartIds                                = array_map('trim', explode(',', $orders->cart_id));
        // $shipping_address_id = json_decode($orders->shipping_address_id, true);
        $cartDetails                            = Cart::whereIn('cart_id', $cartIds)->with('productdetails')->get();
        $deliveryDetails                        = Shipping_Address::where('shipping_address_id',$shipping_address_id)->get();
        $customer_name                          = $deliveryDetails[0]->first_name . ' ' . $deliveryDetails[0]->last_name;
        $total_amount                           = $orders->subtotal;
        $promocode_value                        = $orders->promocode_value;

        if ($cartDetails) {
            $extractedCardDetails               = [];
            foreach ($cartDetails as $cartDetail) {
                $productDetails                 = [];
                $cartProductDetails             = $cartDetail->productdetails;
                foreach ($cartProductDetails as $product) {
                    $productDetails[] = [
                        'product_id'            => $product->product_id,
                        'product_name'          => $product->product_name,
                        'product_actual_price'  => $product->product_actual_price,
                        'product_discount_price'=> $product->product_discount_price,
                    ];
                } 
    
                $extractedCardDetails[] = [
                    'product_actual_price'      => $cartDetail->product_actual_price,
                    'product_discount_price'    => $cartDetail->product_discount_price,
                    'quantity'                  => $cartDetail->quantity,
                    'size_name'                 => $cartDetail->size_name,
                    'chest'                     => $cartDetail->chest,
                    'blouse'                    => $cartDetail->blouse,
                    'skirt'                     => $cartDetail->skirt,
                    'age'                       => $cartDetail->age,
                    'silkchest'                 => $cartDetail->silkchest,
                    'Waist'                     => $cartDetail->Waist,
                    'ankle'                     => $cartDetail->ankle,
                    'productdetailsemail'       => $productDetails,
                ];

            }
            log::info($extractedCardDetails);
    
    
        } else {
            log::error('Cart details are null for order ID: ' . $order_id);
        }
        // Send the email to multiple recipients
        try {
            Mail::to($toEmail)->send(new OrderConfirmedMail(
                $fromEmail, $toEmail, $ccEmails, $order_id, $customer_name, $extractedCardDetails, $order_status, $delivery_charge, $over_all_total, $deliveryDetails, $order_description,$total_amount,$promocode_value
            ));
            return response()->json(['status' => true,'message'=>'Email sent successfully' ],200);
        } catch (\Exception $e) {
            return response()->json(['status' => false, 'message' => 'Error sending email.', 'data' => $e->getMessage()]);
        }
    }

    // Web Order Process mail
    public function orderProcessedMail(Request $request)         // Web Mail Order Success Mail
    {
        $fromEmail                              = 'karthichronoinfotech@gmail.com';
        $toEmail                                = $request->input('toemails');
        // $ccEmails                               = $request->input('ccEmail');
        $order_id                               = $request->input('order_id');
        $orders                                 = Order::where('order_id', $order_id)->with('shippingaddressdetails')->first();
        $order_description                      = $orders->order_description;
        $delivery_charge                        = '100.00';
        $over_all_total                         = $orders->over_all_total;
        $order_status                           = $orders->order_status;
        $shipping_address_id                    = $orders->shipping_address_id;
        $cartIds                                = array_map('trim', explode(',', $orders->cart_id));
        // $shipping_address_id = json_decode($orders->shipping_address_id, true);
        $cartDetails                            = Cart::whereIn('cart_id', $cartIds)->with('productdetails')->get();
        $deliveryDetails                        = Shipping_Address::where('shipping_address_id',$shipping_address_id)->get();
        $customer_name                          = $deliveryDetails[0]->first_name . ' ' . $deliveryDetails[0]->last_name;
        $total_amount                           = $orders->subtotal;
        $promocode_value                        = $orders->promocode_value;

        if ($cartDetails) {
            $extractedCardDetails               = [];
            foreach ($cartDetails as $cartDetail) {
                $productDetails                 = [];
                $cartProductDetails             = $cartDetail->productdetails;
                foreach ($cartProductDetails as $product) {
                    $productDetails[] = [
                        'product_id'            => $product->product_id,
                        'product_name'          => $product->product_name,
                        'product_actual_price'  => $product->product_actual_price,
                        'product_discount_price'=> $product->product_discount_price,
                    ];
                } 
    
                $extractedCardDetails[] = [
                    'product_actual_price'      => $cartDetail->product_actual_price,
                    'product_discount_price'    => $cartDetail->product_discount_price,
                    'quantity'                  => $cartDetail->quantity,
                    'size_name'                 => $cartDetail->size_name,
                    'chest'                     => $cartDetail->chest,
                    'blouse'                    => $cartDetail->blouse,
                    'skirt'                     => $cartDetail->skirt,
                    'age'                       => $cartDetail->age,
                    'silkchest'                 => $cartDetail->silkchest,
                    'Waist'                     => $cartDetail->Waist,
                    'ankle'                     => $cartDetail->ankle,
                    'productdetailsemail'       => $productDetails,
                ];

            }
            log::info($extractedCardDetails);
    
    
        } else {
            log::error('Cart details are null for order ID: ' . $order_id);
        }
        // Send the email to multiple recipients
        try {
            Mail::to($toEmail)->send(new OrderProcessingMail(
                $fromEmail, $toEmail, $order_id, $customer_name, $extractedCardDetails, $order_status, $delivery_charge, $over_all_total, $deliveryDetails, $order_description,$total_amount,$promocode_value
            ));
            return response()->json(['status' => true,'message'=>'Email sent successfully' ],200);
        } catch (\Exception $e) {
            return response()->json(['status' => false, 'message' => 'Error sending email.', 'data' => $e->getMessage()]);
        }
    }

    // Web  order Shipped Mail mail
    public function orderShippedMail(Request $request)         // Web Mail Order Success Mail
    {
        $fromEmail                              = 'karthichronoinfotech@gmail.com';
        $toEmail                                = $request->input('toemails');
        $tracking_number                        = $request->input('tracking_number');
        $tracking_link                          = $request->input('tracking_link');
        $order_id                               = $request->input('order_id');
        $orders                                 = Order::where('order_id', $order_id)->with('shippingaddressdetails')->first();
        $order_description                      = $orders->order_description;
        $delivery_charge                        = '100.00';
        $over_all_total                         = $orders->over_all_total;
        $order_status                           = $orders->order_status;
        $shipping_address_id                    = $orders->shipping_address_id;
        $cartIds                                = array_map('trim', explode(',', $orders->cart_id));
        // $shipping_address_id = json_decode($orders->shipping_address_id, true);
        $cartDetails                            = Cart::whereIn('cart_id', $cartIds)->with('productdetails')->get();
        $deliveryDetails                        = Shipping_Address::where('shipping_address_id',$shipping_address_id)->get();
        $customer_name                          = $deliveryDetails[0]->first_name . ' ' . $deliveryDetails[0]->last_name;
        $total_amount                           = $orders->subtotal;
        $promocode_value                        = $orders->promocode_value;

        if ($cartDetails) {
            $extractedCardDetails               = [];
            foreach ($cartDetails as $cartDetail) {
                $productDetails                 = [];
                $cartProductDetails             = $cartDetail->productdetails;
                foreach ($cartProductDetails as $product) {
                    $productDetails[] = [
                        'product_id'            => $product->product_id,
                        'product_name'          => $product->product_name,
                        'product_actual_price'  => $product->product_actual_price,
                        'product_discount_price'=> $product->product_discount_price,
                    ];
                } 
    
                $extractedCardDetails[] = [
                    'product_actual_price'      => $cartDetail->product_actual_price,
                    'product_discount_price'    => $cartDetail->product_discount_price,
                    'quantity'                  => $cartDetail->quantity,
                    'size_name'                 => $cartDetail->size_name,
                    'chest'                     => $cartDetail->chest,
                    'blouse'                    => $cartDetail->blouse,
                    'skirt'                     => $cartDetail->skirt,
                    'age'                       => $cartDetail->age,
                    'silkchest'                 => $cartDetail->silkchest,
                    'Waist'                     => $cartDetail->Waist,
                    'ankle'                     => $cartDetail->ankle,
                    'productdetailsemail'       => $productDetails,
                ];

            }
            log::info($extractedCardDetails);
    
    
        } else {
            log::error('Cart details are null for order ID: ' . $order_id);
        }
        // Send the email to multiple recipients
        try {
            Mail::to($toEmail)->send(new OrderShippedMail(
                $fromEmail, $toEmail, $order_id, $customer_name, $extractedCardDetails, $order_status, $delivery_charge, $over_all_total, $deliveryDetails, $order_description, $total_amount, $tracking_link, $tracking_number,$promocode_value));
            return response()->json(['status' => true,'message'=>'Email sent successfully' ],200);
        } catch (\Exception $e) {
            return response()->json(['status' => false, 'message' => 'Error sending email.', 'data' => $e->getMessage()]);
        }
    }

    // Web Order Delivered mail
    public function orderDeliveredMail(Request $request)         // Web Mail Order Success Mail
    {
        $fromEmail                              = 'karthichronoinfotech@gmail.com';
        $toEmail                                = $request->input('toemails');
        $ccEmails                               = $request->input('ccEmail');
        $order_id                               = $request->input('order_id');
        $orders                                 = Order::where('order_id', $order_id)->with('shippingaddressdetails')->first();
        $order_description                      = $orders->order_description;
        $delivery_charge                        = '100.00';
        $over_all_total                         = $orders->over_all_total;
        $order_status                           = $orders->order_status;
        $shipping_address_id                    = $orders->shipping_address_id;
        $cartIds                                = array_map('trim', explode(',', $orders->cart_id));
        // $shipping_address_id = json_decode($orders->shipping_address_id, true);
        $cartDetails                            = Cart::whereIn('cart_id', $cartIds)->with('productdetails')->get();
        $deliveryDetails                        = Shipping_Address::where('shipping_address_id',$shipping_address_id)->get();
        $customer_name                          = $deliveryDetails[0]->first_name . ' ' . $deliveryDetails[0]->last_name;
        $total_amount                           = $orders->subtotal;
        $promocode_value                        = $orders->promocode_value;
        if ($cartDetails) {
            $extractedCardDetails               = [];
            foreach ($cartDetails as $cartDetail) {
                $productDetails                 = [];
                $cartProductDetails             = $cartDetail->productdetails;
                foreach ($cartProductDetails as $product) {
                    $productDetails[] = [
                        'product_id'            => $product->product_id,
                        'product_name'          => $product->product_name,
                        'product_actual_price'  => $product->product_actual_price,
                        'product_discount_price'=> $product->product_discount_price,
                    ];
                } 
    
                $extractedCardDetails[] = [
                    'product_actual_price'      => $cartDetail->product_actual_price,
                    'product_discount_price'    => $cartDetail->product_discount_price,
                    'quantity'                  => $cartDetail->quantity,
                    'size_name'                 => $cartDetail->size_name,
                    'chest'                     => $cartDetail->chest,
                    'blouse'                    => $cartDetail->blouse,
                    'skirt'                     => $cartDetail->skirt,
                    'age'                       => $cartDetail->age,
                    'silkchest'                 => $cartDetail->silkchest,
                    'Waist'                     => $cartDetail->Waist,
                    'ankle'                     => $cartDetail->ankle,
                    'productdetailsemail'       => $productDetails,
                ];

            }
            log::info($extractedCardDetails);
    
    
        } else {
            log::error('Cart details are null for order ID: ' . $order_id);
        }
        // Send the email to multiple recipients
        try {
            Mail::to($toEmail)->send(new OrderDeliveredMail(
                $fromEmail, $toEmail, $ccEmails, $order_id, $customer_name, $extractedCardDetails, $order_status, $delivery_charge, $over_all_total, $deliveryDetails, $order_description,$total_amount, $promocode_value
            ));
            return response()->json(['status' => true,'message'=>'Email sent successfully' ],200);
        } catch (\Exception $e) {
            return response()->json(['status' => false, 'message' => 'Error sending email.', 'data' => $e->getMessage()]);
        }
    }

    // Canceld Order Mail
    public function orderCanceledMail(Request $request)         // Web Mail Order Success Mail
    {
        $fromEmail                              = 'karthichronoinfotech@gmail.com';
        $toEmail                                = $request->input('toemails');
        $order_id                               = $request->input('order_id');
        $orders                                 = Order::where('order_id', $order_id)->with('shippingaddressdetails')->first();
        $order_description                      = $orders->order_description;
        $delivery_charge                        = '100.00';
        $over_all_total                         = $orders->over_all_total;
        $order_status                           = $orders->order_status;
        $shipping_address_id                    = $orders->shipping_address_id;
        $cartIds                                = array_map('trim', explode(',', $orders->cart_id));
        // $shipping_address_id = json_decode($orders->shipping_address_id, true);
        $cartDetails                            = Cart::whereIn('cart_id', $cartIds)->with('productdetails')->get();
        $deliveryDetails                        = Shipping_Address::where('shipping_address_id',$shipping_address_id)->get();
        $customer_name                          = $deliveryDetails[0]->first_name . ' ' . $deliveryDetails[0]->last_name;
        $total_amount                           = $orders->subtotal;
        $promocode_value                        = $orders->promocode_value;
        if ($cartDetails) {
            $extractedCardDetails               = [];
            foreach ($cartDetails as $cartDetail) {
                $productDetails                 = [];
                $cartProductDetails             = $cartDetail->productdetails;
                foreach ($cartProductDetails as $product) {
                    $productDetails[] = [
                        'product_id'            => $product->product_id,
                        'product_name'          => $product->product_name,
                        'product_actual_price'  => $product->product_actual_price,
                        'product_discount_price'=> $product->product_discount_price,
                    ];
                } 
    
                $extractedCardDetails[] = [
                    'product_actual_price'      => $cartDetail->product_actual_price,
                    'product_discount_price'    => $cartDetail->product_discount_price,
                    'quantity'                  => $cartDetail->quantity,
                    'size_name'                 => $cartDetail->size_name,
                    'chest'                     => $cartDetail->chest,
                    'blouse'                    => $cartDetail->blouse,
                    'skirt'                     => $cartDetail->skirt,
                    'age'                       => $cartDetail->age,
                    'silkchest'                 => $cartDetail->silkchest,
                    'Waist'                     => $cartDetail->Waist,
                    'ankle'                     => $cartDetail->ankle,
                    'productdetailsemail'       => $productDetails,
                ];

            }
            log::info($extractedCardDetails);
    
    
        } else {
            log::error('Cart details are null for order ID: ' . $order_id);
        }
        // Send the email to multiple recipients
        try {
            Mail::to($toEmail)->send(new OrderCanceledMail(
                $fromEmail, $toEmail, $order_id, $customer_name, $extractedCardDetails, $order_status, $delivery_charge, $over_all_total, $deliveryDetails, $order_description, $total_amount, $tracking_link, $tracking_number, $promocode_value));
            return response()->json(['status' => true,'message'=>'Email sent successfully' ],200);
        } catch (\Exception $e) {
            return response()->json(['status' => false, 'message' => 'Error sending email.', 'data' => $e->getMessage()]);
        }
    }
}
