const {
  mockCheckoutServicev3,
  checkoutServicev2,
} = require("../../helpers/checkout.service");

exports.mockCheckout = async (req, res, next) => {
  try {
    const { customerId } = req;
    const {
      shipping_type,
      payment_method,
      rpaf_payment,
      bank_payment,
      voucher_code,
      pickup_date,
      order_instructions,
      agent_code,
      transportify_shipping_fee,
      lalamove_shipping_fee,
      checkoutAddress,
      checkoutLatitude,
      checkoutLongitude,
      suggested_voucher_id,
    } = req.body;

    const serviceResponse = await mockCheckoutServicev3(
      customerId,
      {
        shipping_type,
        payment_method,
        buildcredit_payment: rpaf_payment,
        bank_payment,
        voucher_code,
        pickup_date,
        order_instructions,
        agent_code,
        transportify_shipping_fee,
        lalamove_shipping_fee,
        checkoutAddress,
        checkoutLatitude,
        checkoutLongitude,
        suggested_voucher_id,
      },
      req,
    );

    if (serviceResponse.error) {
      const itemStockError = serviceResponse.itemStockError || false;
      return res.status(200).json({
        success: false,
        response: serviceResponse.error,
        itemStockError,
      });
    }
    res.status(200).json({ success: true, response: serviceResponse });
  } catch (err) {
    next(err);
  }
};
exports.checkout = async (req, res, next) => {
  try {
    const { customerId } = req;
    const { data } = req.body;

    const {
      shipping_type,
      payment_method,
      rpaf_payment,
      bank_payment,
      voucher_code,
      pickup_date,
      order_instructions,
      agent_code,
      transportify_shipping_fee,
      lalamove_shipping_fee,
      lalamove_quotation_id,
      lalamove_customer_stop_id,
      lalamove_seller_stop_id,
      checkoutAddress,
      checkoutLatitude,
      checkoutLongitude,
      suggested_voucher_id,
    } = data;

    const { note, po_attachment } = req.files;

    const attachmentFileName =
      note && Array.isArray(note) && note[0].filename ? note[0].filename : null;

    const poAttachmentFileName =
      po_attachment && Array.isArray(po_attachment) && po_attachment[0].filename
        ? po_attachment[0].filename
        : null;

    const serviceResponse = await checkoutServicev2(
      customerId,
      {
        shipping_type,
        payment_method,
        buildcredit_payment: rpaf_payment,
        bank_payment,
        voucher_code,
        pickup_date,
        order_instructions,
        agent_code,
        transportify_shipping_fee,
        lalamove_shipping_fee,
        lalamove_quotation_id,
        lalamove_customer_stop_id,
        lalamove_seller_stop_id,
        checkoutAddress,
        checkoutLatitude,
        checkoutLongitude,
        bank_transfer_attachment: attachmentFileName,
        suggested_voucher_id,
        poAttachmentFileName,
      },
      req,
    );

    if (serviceResponse.error) {
      const itemStockError = serviceResponse.itemStockError || false;

      return res
        .status(200)
        .json({
          success: false,
          response: serviceResponse.error,
          itemStockError,
        });
    }
    res.status(200).json({ success: true, response: serviceResponse });
  } catch (err) {
    next(err);
  }
};
