const { applyBBPSOffer } = require('../model/bbpsOfferModel');

async function handleApplyBBPSOffer(req, res, next) {
  try {
    const { pmobile, uid, pemail, validationid, totalAmount, offerCode } = req.body;

    // validation
    if (![pmobile, uid, pemail, validationid, totalAmount, offerCode].every(Boolean)) {
      return res.status(400).json({
        success: false,
        message: 'Missing required fields.'
      });
    }

    if (uid === 'Not Applicable') {
      return res.status(401).json({
        success: false,
        message: 'Please login to apply offer.'
      });
    }

    const result = await applyBBPSOffer({
      pmobile,
      uid,
      pemail,
      validationid,
      totalAmount: parseFloat(totalAmount),
      offerCode: offerCode.trim()
    });

    if (!result.success) {
      return res.status(400).json(result);
    }

    res.status(200).json(result);

  } catch (err) {
    next(err);
  }
}

module.exports = { handleApplyBBPSOffer };