// app.post("/checkout-session", handlePayment);
import bodyParser from "body-parser";
const { STRIPE_SECRET_KEY, WEBHOOK_SECRET_KEY } = process.env;
import crypto from "crypto";
import Stripe from "stripe";
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY);
// const stripe = require("stripe")("Your Secret Key");
import { Order } from "./orderModel.js";

import ShippingAddress from "../ShippingAddresses/ShippingAddressModel.js";
import sendEmail from "../../Utils/sendEmail.js";
// const endpointSecret = STRIPE_SECRET_KEY;
//generate unique order id
export const generateUniqueOrderId = async () => {
  const currentYear = new Date().getFullYear();
  // Find the latest order to get the last serial number
  const latestOrder = await Order.findOne({}, {}, { sort: { orderID: -1 } });
  let serialNumber = 1;

  if (latestOrder) {
    const lastYear = parseInt(latestOrder.orderID.substring(0, 4), 10);
    if (lastYear === currentYear) {
      // If the last order was in the current year, increment the serial number
      serialNumber = parseInt(latestOrder.orderID.substring(4), 10) + 1;
    }
  }
  // Pad the serial number with zeros and concatenate with the current year
  const paddedSerialNumber = serialNumber.toString().padStart(7, "0");
  const orderId = `${currentYear}${paddedSerialNumber}`;
  return orderId;
};

export const handlePayment = async (req, res) => {
  try {
    const { email } = req.user;
    if (!email)
      return res.status(400).send({ message: "Please enter the email" });
    const { address, cart, subtotal } = req.body;

    if (cart.length < 1)
      return res.status(400).json({ message: "cart is empty!" });
    switch (true) {
      //validation
      case !address: {
        return res.status(404).json({ msg: "please provide shipping address" });
      }
      case !subtotal: {
        return res.status(404).json({ msg: "please provide product subtotal" });
      }
    }
    let addss = await ShippingAddress.findById(address);

    let shipping = {
      Name: addss.Name,
      phoneNumber: addss.phoneNumber,
      street: addss.street,
      city: addss.city,
      state: addss.state,
      postalCode: addss?.postalCode,
      country: addss.country,
      addressId: address,
    };
    const orderItems = await cart.map((item) => ({
      product: item.product._id,
      name: item.product.name,
      price: item.product.total_amount,
      image: item.product.image,
      quantity: item.quantity,
      product_Subtotal: item.subtotal,
    }));

    // console.log("line", lineItems[0]);
    const Id = await generateUniqueOrderId();
    const order = await Order.create({
      orderID: Id,
      total_amount: subtotal,
      orderItems,
      shippingInfo: shipping,
      user: req.user._id,
    });
    // console.log("fffffffff", order, "llllllllll");
    const lineItems = await cart.map((item) => ({
      price_data: {
        currency: "inr",
        product_data: {
          name: item.product.name,

          images: [item.product.image[0]?.url],
        },
        unit_amount: Number(item.product.total_amount) * 100,
      },
      quantity: Number(item.quantity),
    }));
    if (order) {
      const session = await stripe.checkout.sessions.create({
        payment_method_types: ["card"],
        line_items: lineItems,
        mode: "payment",
        customer_email: `${email}`,
        metadata: {
          orderId: order._id.toString(),

          // Add any other key-value pairs as needed
        },
        shipping_address_collection: {
          allowed_countries: ["IN"],
          // Allow only India for INR transactions
        },
        billing_address_collection: "required",
        success_url: `${process.env.FRONTEND_URL}/order-complete`, // Provide your success URL here
        cancel_url: `${process.env.FRONTEND_URL}/cart`,
      });
      //   res.json({ sessionId: session.id });

      res
        .status(200)
        .send({ message: "order created", url: session.url, id: session.id });
    }
  } catch (err) {
    console.log(err);
    res.status(500).send({ message: "Something went wrong", err });
  }
};

export const webhook = async (req, res) => {
  const webhookSecret = process.env.STRIPE_WEBHOOK_SECRET;

  const signature = req.headers["stripe-signature"];
  let event;
  if (webhookSecret) {
    try {
      event = stripe.webhooks.constructEvent(
        req.body,
        signature,
        webhookSecret
      );
    } catch (err) {
      console.log(`❌ Error message: ${err.message}`);
      res.status(400).send(`Webhook Error: ${err.message}`);
      return;
    }
  }

  if (event.type === "checkout.session.completed") {
    const findOrder = await Order.findById(event.data.object.metadata?.orderId);
    findOrder.paypal_payer_id = event.data.object.id;
    findOrder.paidAt = new Date(event.data.object.created * 1000);
    findOrder.isPaid = true;
    if (event.data.object?.payment_status === "paid") {
      findOrder.payment_status = "success";
    } else {
      findOrder.payment_status = "failed";
    }
    findOrder.orderStatus = "new";
    await findOrder.save();

    // Construct the HTML for the email
    const itemRows = findOrder?.orderItems
      .map(
        (item) =>
          `<tr><td>${item?.name}</td><td>${item?.quantity}</td><td>₹${item?.price}</td></tr>`
      )
      .join("");

    const htmlContent = `
    
    <strong style="color: #1b03a3; font-size: 16px"> Hi ${findOrder?.shippingInfo?.first_Name},</strong>
    
    <p style="color: #555; font-size: 15px;">Great news! Your order #${findOrder?.orderID} has been confirmed. Here are the details:</p>
    <br/>
    
    <table border="1" cellpadding="5" style="border-collapse: collapse; width: 100%;">
      <thead>
        <tr>
          <th>Item</th>
          <th>Quantity</th>
          <th>Price</th>
        </tr>
      </thead>
      <tbody>
        ${itemRows}
      </tbody>
    </table>
    
    <p style="color: #555; font-size: 15px;">Shipping Address: ${findOrder?.shippingInfo.first_Name} ${findOrder?.shippingInfo.last_Name},${findOrder?.shippingInfo.postalCode},
    ${findOrder?.shippingInfo.street},
    ${findOrder?.shippingInfo.city},
    ${findOrder?.shippingInfo.state},
    ${findOrder?.shippingInfo.country} </br> Phone number:${findOrder?.shippingInfo.phone_Number}</p>
    <p style="color: #555; font-size: 15px;">Total: ₹${findOrder.total_amount}</p>
    
    <br/>
    <span style="color: #555; font-size: 13px;">Best regards,</span><br/>
    <span style="color: #555; font-size: 13px;">Team Smellika</span>`;

    // Send the email
    await sendEmail({
      to: `${event.data.object.customer_email}`, // Change to your recipient
      from: `${process.env.SEND_EMAIL_FROM}`, // Change to your verified sender
      subject: `Your Order #${findOrder?.orderID} Confirmation`,
      html: htmlContent,
    });

    // Items: [List of Purchased Items]
    // Total Amount: [Total Amount]
    // Shipping Address: [Shipping Address]

    // We'll keep you updated on the shipping progress. Thanks for choosing Smellika!

    // Best regards
    // Team Smellika
    console.log(
      "event.data.object",
      event.data.object,
      "---------------------"
    );

    // console.log(`💰 Payment status: ${event.data.object?.payment_status}`);

    // Saving the payment details in the database
    // const payment = await Payment.create({
    //   customer_email: event.data.object.customer_email,
    //   amount: event.data.object.amount_total / 100,
    //   paymentId: event.data.object.id,
    //   paymentStatus: event.data.object.payment_status,
    //   createdAt: event.data.object.created,
    // });
  }
  // if (event.type === "checkout.session.completed") {
  //   console.log("dddddddddddd", event.data);
  //   console.log("event.data.object", event.data.object);
  //   console.log(`💰 Payment status: ${event.data.object?.payment_status}`);
  //   payment_intent.payment_failed;

  //   // Saving the payment details in the database
  //   // const payment = await Payment.create({
  //   //   customer_email: event.data.object.customer_email,
  //   //   amount: event.data.object.amount_total / 100,
  //   //   paymentId: event.data.object.id,
  //   //   paymentStatus: event.data.object.payment_status,
  //   //   createdAt: event.data.object.created,
  //   // });
  // }

  // Return a 200 res to acknowledge receipt of the event
  res.status(200).end();
  //   res.send().end();
};
