import { TransactionEntity } from 'src/modules/transaction/transaction/domain/entities/transaction.entity';
import { InvoiceType, PhoneNumber } from '../../../constants';
import { PaymentMethodEntity } from 'src/modules/transaction/payment-method/domain/entities/payment-method.entity';

export function mappingHeader(transaction: TransactionEntity, invoiceType) {
  return [
    {
      stack: [
        {
          alignment: 'center',
          columns: [
            {
              width: 100,
              image: 'data',
            },
            {
              width: 'auto',
              text: 'have',
              fontSize: 50,
              bold: true,
              color: '#169f54',
              padding: [0, 0, 0, 0],
              margin: [0, 0, 0, 0],
              alignment: 'center',
            },
            {
              width: 'auto',
              text: 'fun',
              fontSize: 50,
              bold: true,
              color: '#61c4eb',
              padding: [0, 0, 0, 0],
              margin: [0, 0, 0, 0],
              // alignment: "center"
            },
          ],
        },
        {
          text: invoiceType.toUpperCase(),
          fontSize: 30,
          alignment: 'center',
        },
        {
          columns: [
            {
              text: transaction.invoice_code,
            },
            {
              text: new Date(transaction.booking_date).toDateString(),
              alignment: 'right',
            },
          ],
        },
      ],
    },
    {
      width: 150,
      text: 'Jl, Kolonel Masturi No.KM. 11, \n Kertawangi, Kec. Cisatua, \n Kab. Bandung Barat, \n Jawa Barat 40551 \n 0815-6380-8021',
      //   color: '#aaaaab',
      //   bold: true,
      fontSize: 11,
      margin: [0, 20, 0, 5],
    },
  ];
}

export function mappingFooter() {
  return [
    [
      {
        text: 'we commits to prividing an educative, \n playful, and purposeful environment\n this is advantageous for all ages',
        border: [false, true, false, false],
        margin: [0, 5, 0, 5],
        alignment: 'right',
      },
      {
        text: 'Thank \n You',
        border: [false, true, false, false],
        fontSize: 20,
        bold: true,
        color: '#169f54',
        margin: [0, 5, 0, 5],
      },
    ],
  ];
}

export function mappingPrice(
  transaction: TransactionEntity,
  invoiceType: InvoiceType,
) {
  const result = [];
  const totalData = [
    InvoiceType.REFUND_CONFIRMATION,
    InvoiceType.REFUND_REQUEST,
  ].includes(invoiceType)
    ? transaction['refund'].refund_total
    : Number(transaction.payment_total).toLocaleString('id-ID', {
        style: 'currency',
        currency: 'IDR',
      });
  const subTotalData = [
    InvoiceType.REFUND_CONFIRMATION,
    InvoiceType.REFUND_REQUEST,
  ].includes(invoiceType)
    ? transaction['refund'].refund_total
    : Number(transaction.payment_sub_total).toLocaleString('id-ID', {
        style: 'currency',
        currency: 'IDR',
      });

  const sub_total = [
    {
      text: 'SUBTOTAL',
      alignment: 'right',
      bold: true,
      margin: [0, 5, 0, 5],
    },
    {
      text: subTotalData,
      margin: [0, 5, 0, 5],
    },
  ];
  result.push(sub_total);

  if (Number(transaction.payment_discount_total ?? 0) > 0) {
    const discount = [
      {
        text: 'DISCOUNT',
        bold: true,
        alignment: 'right',
        margin: [0, 5, 0, 5],
      },
      {
        text: Number(transaction.payment_discount_total).toLocaleString(
          'id-ID',
          {
            style: 'currency',
            currency: 'IDR',
          },
        ),
        margin: [0, 5, 0, 5],
      },
    ];
    result.push(discount);
  }

  const total = [
    {
      text: 'TOTAL',
      bold: true,
      border: [false, false, false, true],
      alignment: 'right',
      margin: [0, 5, 0, 5],
    },
    {
      text: totalData,
      border: [false, false, false, true],
      margin: [0, 5, 0, 5],
    },
  ];
  result.push(total);

  return result;
}

export function mappingItem(transaction, invoiceType: InvoiceType) {
  const header = [
    {
      text: 'ITEM',
      border: [false, true, false, false],
      margin: [0, 5, 0, 5],
      bold: true,
      textTransform: 'uppercase',
    },
    {
      text: 'QTY',
      alignment: 'center',
      border: [false, true, false, false],
      margin: [0, 5, 0, 5],
      bold: true,
      textTransform: 'uppercase',
    },
    {
      text: 'PRICE',
      alignment: 'center',
      border: [false, true, false, false],
      margin: [0, 5, 0, 5],
      bold: true,
      textTransform: 'uppercase',
    },
    {
      text: 'AMOUNT',
      border: [false, true, false, false],
      alignment: 'center',
      bold: true,
      margin: [0, 5, 0, 5],
      textTransform: 'uppercase',
    },
  ];

  const result = [];

  if (
    [InvoiceType.REFUND_CONFIRMATION, InvoiceType.REFUND_REQUEST].includes(
      invoiceType,
    )
  ) {
    transaction.refund?.refund_items
      ?.filter((item) => Number(item.qty_refund) > 0)
      .forEach((item) => {
        const dataRow = [
          {
            text: item.transaction_item.item_name,
            margin: [0, 5, 0, 5],
            alignment: 'left',
          },
          {
            text: item.qty_refund,
            margin: [0, 5, 0, 5],
            alignment: 'center',
          },
          {
            text: Number(item.transaction_item.total_price).toLocaleString(
              'id-ID',
              {
                style: 'currency',
                currency: 'IDR',
              },
            ),
            margin: [0, 5, 0, 5],
            alignment: 'right',
          },
          {
            text: Number(item.refund_total).toLocaleString('id-ID', {
              style: 'currency',
              currency: 'IDR',
            }),
            alignment: 'right',
            margin: [0, 5, 0, 5],
          },
        ];
        result.push(dataRow);
      });
  } else {
    transaction.items.forEach((item) => {
      const dataRow = [
        {
          text: item.item_name,
          margin: [0, 5, 0, 5],
          alignment: 'left',
        },
        {
          text: item.qty,
          margin: [0, 5, 0, 5],
          alignment: 'center',
        },
        {
          text: Number(item.item_price).toLocaleString('id-ID', {
            style: 'currency',
            currency: 'IDR',
          }),
          margin: [0, 5, 0, 5],
          alignment: 'right',
        },
        {
          text: Number(item.total_price).toLocaleString('id-ID', {
            style: 'currency',
            currency: 'IDR',
          }),
          alignment: 'right',
          margin: [0, 5, 0, 5],
        },
      ];
      result.push(dataRow);
    });
  }

  const body = [header, ...result];

  return body;
}

export function mappingBody(
  transaction: TransactionEntity,
  invoiceType: InvoiceType,
) {
  // booking date change information
  if (invoiceType == InvoiceType.BOOKING_DATE_CHANGE) {
    return [
      "Great news! We've successfully updated your booking date as per your request. \n We're exited to accommodate your new plans and ensure everything goes smoothly \n\n",
      'Here are your updated booking details:',
      {
        text: `\n\n Original Booking Date: ${new Date(
          transaction['booking_date_before'],
        ).toDateString()} \n New Booking Date: ${new Date(
          transaction.booking_date,
        ).toDateString()} \n\n`,
        bold: true,
      },
      "Here's a quick recap of your order :",
    ];
  }

  // booking invoice
  else if (invoiceType == InvoiceType.BOOKING_INVOICE) {
    return [
      "Thank you for choosing us! We're absolutely thrilled and can't wait to embark on this exiting day with you. See you soon for fun times ahead!",
    ];
  } else if (invoiceType == InvoiceType.PAYMENT_CONFIRMATION) {
    // booking payment confirmation
    return [
      'We are exited to inform you that your payment has been successfully received! \n',
      'Attached to this email, you will find your confirmation receipt. \n',
      'Please keep this safe as you will need to show it at the entrance upon your arrival. \n',
      "It's your golden ticket to all the fun and excitement awaiting you \n\n",
      "Here's a quick recap: \n",
    ];
  }

  // expired information invoice
  // else if (invoiceType == InvoiceType.INVOICE_EXPIRED) {

  //   return [
  //     "We hope this message finds you well!",
  //     "Uh-oh! it looks like your invoice, dated 15 Juli, has officially expired as of 15 Juli. But no worries, we can fix this together \n",
  //     "To keep the goof times rolling, our friendly support team is just a call away at \n",
  //     "0564645 \n\n",
  //     "Here are the detail of the expired invoice: "

  //   ]
  // }

  // refund information
  else if (invoiceType == InvoiceType.REFUND_REQUEST) {
    return [
      "We'ew trully sorry for any inconvenience that led to this request. \n",
      "We've received your refund request for: \n",
    ];
  }

  // refund confirmation
  else if (invoiceType == InvoiceType.REFUND_CONFIRMATION) {
    return [
      'Good news! \n',
      "We've successfully processed your refund for: \n",
    ];
  }
}

export function mappingBodyBottom(
  transaction: TransactionEntity,
  invoiceType: InvoiceType,
  banks: PaymentMethodEntity[],
) {
  if (invoiceType == InvoiceType.BOOKING_DATE_CHANGE) {
    // booking date change information
    return [
      "For your convenience, we've attached a new confirmation receipt reflecting these changes \n",
      'Please be sure to bring this updated receipt with you on the new date \n\n',
      'If you have any questions or need further assistance, our friendly support team is just a call away at \n\n',
      PhoneNumber,
      "\nThank you and we can't wait to see you and make sure you hove an amazing time!",
    ];
  } else if (invoiceType == InvoiceType.BOOKING_INVOICE) {
    // booking invoice
    return [
      'Just a friendly reminder that your invoice will expire on 24 Agustus 2024 \n',
      'To keep things running smoothly, please ensure your payment is completed before this data. \n',
      '\nFor youe convenience, here is a list of our account details \n\n',
      {
        text: [
          banks.forEach((bank) => {
            return {
              text: `${bank.issuer_name} ${bank.account_number} a/n ${bank.account_name} \n`,
              bold: true,
            };
          }),
        ],
      },
      "\n Once you've made the payment, please kindly email or send the proof of payment so we can proceed with your booking promptly\n",
      'If you have any questions or need assistance, feel free to reach out to our support team at\n',
      PhoneNumber,
    ];
  }

  // booking payment confirmation
  else if (invoiceType == InvoiceType.PAYMENT_CONFIRMATION) {
    return [
      'If you have any questions or need assistance, feel free to reach out to our support team at\n',
      PhoneNumber,
      "\nDon't forget to bring a smile and your confirmation receipt (attached) for a smooth entry. \n",
      "We can't wait to see you and ensure you have an amazing time with us!",
    ];
  }

  // expired information invoice
  else if (invoiceType == InvoiceType.INVOICE_EXPIRED) {
    return [];
  }

  // refund information
  else if (invoiceType == InvoiceType.REFUND_REQUEST) {
    return [
      "Your satisfaction is important to us, and we're commited to resolving this as quickly as possible.\n",
      'Our team is already on it and will process your refund request promptly. \n',
      "We'll keep you updated and notify you once the refund has been processed. \n",
      "If you have any questions or need futher assistance, don't hestitate to reach out us at \n",
      PhoneNumber,
      '\n\n',
      'Thank you for your patience and understanding \n',
      'We appreciate your feedback and here to make things right!',
    ];
  }

  // refund confirmation
  else if (invoiceType == InvoiceType.REFUND_CONFIRMATION) {
    return [
      'Here are the details of your refund: \n\n',
      {
        text: `Transaction Number: ${transaction.invoice_code} \n`,
      },
      {
        text: `Refund Processed:  ${transaction['refund'].refund_date}\n`,
      },
      {
        text: `Bank Account: ${transaction['refund'].bank_name} \n`,
      },
      {
        text: `Account Number: ${transaction['refund'].bank_account_number} \n`,
      },
      {
        text: `Account Name: ${transaction['refund'].bank_account_name} \n`,
      },
      "\n We hope this helps make things righ, and we're he to assist if you need anything else. \n",
      'You should see the refund reflected in your account within 3 Business days \n\n',
      'Thank you for your patience and understanding\n',
      'If you have any questions or need assistance, feel free to reach out to our support team at \n',
      PhoneNumber,
      "\nWe're alyways here to help!",
    ];
  }
}
