const ticketModel = require('../model/ticketModel');
const { verifyCaptcha } = require('../utils/verifyCaptcha.js');

exports.printTicket = async (req, res) => {
  
    // console.log(req.query.tin);
    //const tin = req.query.tin;
    // console.log(tin);
    console.log('Request body:', req.body);
    const { tin, captchaToken } = req.body;
 
    if (!tin && !captchaToken)
    {
        return res.status(400).json({ error: 'Booking Id and Captcha Token are required' });
    }
    else if (!tin) 
    {
        return res.status(422).json({ error: 'Booking Id is required' });
        
    }
    else if (!captchaToken) 
    {
        return res.status(401).json({ error: 'Captcha Token is required' });
        
    }
    else
    {
        const captchaResponse = await verifyCaptcha(captchaToken);
        if (!captchaResponse.success)
        {
            return res.status(403).json({ error: 'Captcha verification failed' });
        }
        console.log("captchaToken", captchaToken);
        const trimmedTin = tin.trim();
        try 
        {
            const ticketDetails = await ticketModel.printTicket(trimmedTin);
            if (!ticketDetails) 
            {
                return res.status(404).json({ error: 'No trips available for the given booking Id' });
            }
            res.json(ticketDetails);
        }
        catch (error)
        {
            console.error('Error ticked details:', error.message);
            res.status(500).json({ error: 'Failed to get ticket details' });
            
        }
    }
    
};

