const fs = require('fs-extra');
const { updateUserData } = require('../utils/user');

/**
 * Handle about company flow
 * @param {Object} sock - WhatsApp socket
 * @param {string} phone - User's phone number
 * @param {Object} userData - User data
 * @param {string} message - User's message (optional)
 */
async function handleAbout(sock, phone, userData, message = '') {
  try {
    // Load company information
    const companyData = await fs.readJson('config/company.json');
    
    // Show company information with logo
    const aboutMessage = `ℹ️ *About ${companyData.name}*\n\n${companyData.description}\n\n🌐 *Website:* ${companyData.website}\n📞 *Support:* ${companyData.supportContact}\n📧 *Email:* ${companyData.email}\n\n🏠 Reply with 'menu' to return to the main menu.`;
    
    await sock.sendMessage(phone, {
      image: { url: companyData.logo },
      caption: aboutMessage
    });
    
    await updateUserData(phone, { state: 'ABOUT_INFO' });
    
    // Handle response if provided
    if (message && message.toLowerCase() === 'menu') {
      await sock.sendMessage(phone, { text: "🏠 Taking you back to the main menu... 🚀" });
      const { sendMainMenu } = require('../utils/menuUtils');
      await sendMainMenu(sock, phone, userData.name);
    }
  } catch (error) {
    console.error('Error in about flow:', error);
    await sock.sendMessage(phone, {
      text: "⚠️ *Oops!* Sorry, I encountered an error while retrieving company information. 🙏 Please try again or type #startover to reset our conversation. 🔄"
    });
  }
}

module.exports = {
  handleAbout
};