// const myBookingsModel = require('../model/myBookingsModel');
const dbConnect   = require('../configs/dbConnection');
const {sendOtp, verifyOtp} = require('../../smsapi/model/smsModel.js');
//const {verifyCaptcha} = require('../utils/verifyCaptcha.js');
// console.log(typeof(vrifyCaptcha));


exports.verifyUser = async (req, res) => {
  try {
    const {mobile, email} = req.body;
      
    //   const {'g-Rrecaptcha-Response': token} = req.body;
    //   const remoteIp = req.headers['x-forwarded-for'] || req.ip;
    //   console.log(mobile, email, token );
    //   console.log(token, remoteIp);
       
    //const captchaResponse = await verifyCaptcha(token, remoteIp);
    
    // if (!captchaResponse.success) {
    //   return res.status(400).json({error:'captcha verification failed'})
    // }
     
   
    
    if (!mobile || !email) {
     return res.status(400).json({ error: 'Mobile number and email ID are required' });
    };
    
    const trimmedMobileNo = mobile.trim();
    const trimmedemailId = email.trim();
    
    const [bookingDetails] = await dbConnect.query(
        `SELECT * FROM uticket WHERE pemail = ? AND pmobile = ? AND tin_ticket <> '0' AND tin_ticket NOT LIKE 'Error%'`,
        [trimmedemailId, trimmedMobileNo]);
        
        if (!bookingDetails || bookingDetails.length === 0) {
        return res.status(200).json({ message: 'No trips available for the given details' });
        }

    const formattedMobileNo = '+91' + trimmedMobileNo.replace(/\D/g, '').replace(/^0+|^91+/, '');
  
    
    const sendOtpResponse = await sendOtp(formattedMobileNo, "BOBROSOTP" );
    res.status(200).json({sendOtpResponse});
    
    } catch (error) {
    console.error('Error on ticked details:', error.message);
    res.status(400).json({ error: 'Failed to verify user' });
  }
};


exports.getBookingsData = async (req, res) => {
  
  const {mobile, email, otp} = req.body;
  if (!mobile || !email || !otp) {
    return res.status(404).json({ error: 'please enter your otp' });
  };
    console.log(mobile, email, otp);
  const trimmedMobleNo = mobile.trim();
  const trimmedemailId = email.trim();
  const trimmedOtp = otp.trim();
  
  const formattedMobleNo = '+91' + trimmedMobleNo.replace(/\D/g, '').replace(/^0|^91/, '');

  try {
    const verifyOtpResponse = await verifyOtp (trimmedMobleNo, otp );
    
    console.log(verifyOtpResponse.Details);
    console.log(verifyOtpResponse.Status);
    
    if (verifyOtpResponse.Details === "OTP Matched"  ) {
        
    // const [bookingDetails] = await dbConnect.query(
    // `SELECT * FROM uticket WHERE pemail = ? AND uid = ? AND tin_ticket <> '0' AND tin_ticket NOT LIKE 'Error%'`,
    // [trimmedemailId, trimmedMobleNo]);
        
    const [bookingDetails] = await dbConnect.query(
        `SELECT * FROM uticket 
        WHERE pemail = ? 
        AND pmobile = ? 
        AND tin_ticket <> '0' 
        AND tin_ticket NOT LIKE 'Error%'`,
        [trimmedemailId, trimmedMobleNo,]);
    
        // AND bdat >= ? 
        // AND pdoj <= ?'
        // startDate, endDate
        
        if (!bookingDetails || bookingDetails.length === 0) {
        return res.status(404).json({ error: 'No trips available for the given details' });
        }
    
        return res.status(200).json({bookingDetails})
      
    } else {
        console.log("can't provide boking details due to otp mismatch");
        res.status(401).json({msg :"otp mismatch"});
    }
    
  } catch (error) {
    console.error('Error in providing booking details:', error.message);
    res.status(500).json({ error: 'Failed to get booking details' });
  }
};


  
  