const config = require('../config/config');

const generateBookingReference = async (Booking) => {
  const now = new Date();
  const year = now.getFullYear();
  const month = String(now.getMonth() + 1).padStart(2, '0');

  let reference;
  let exists = true;

  while (exists) {
    const random = Math.floor(Math.random() * 10000);
    const suffix = String(random).padStart(4, '0');
    reference = `${config.booking.referencePrefix}${year}${month}${suffix}`;
    const existing = await Booking.findOne({ where: { booking_reference: reference } });
    exists = !!existing;
  }

  return reference;
};

const generatePaymentReference = () => {
  const now = new Date();
  const ts = now.getTime().toString(36).toUpperCase();
  const random = Math.floor(Math.random() * 10000).toString(36).toUpperCase();
  return `PAY-${ts}-${random}`;
};

module.exports = { generateBookingReference, generatePaymentReference };
