'use server'

import nodemailer from 'nodemailer';

// Configure SMTP transport using environment variables
const transporter = nodemailer.createTransport({
    host: process.env.EMAIL_HOST || 'smtp.gmail.com', // Replace with actual SMTP host if different
    port: Number(process.env.EMAIL_PORT) || 587,
    secure: (process.env.EMAIL_SECURE === 'true'),
    auth: {
        user: process.env.EMAIL_USER,
        pass: process.env.EMAIL_PASS,
    },
});

export async function submitReservation(formData: FormData) {
    const name = formData.get('name');
    const phone = formData.get('phone');
    const date = formData.get('date');
    const message = formData.get('message');

    // For reservations, we'll log it for now as per current logic
    console.log('Reservation received:', { name, phone, date, message });

    // Simulate delay
    await new Promise(resolve => setTimeout(resolve, 1000));
    return { success: true, message: 'Reservation submitted successfully!' };
}

export async function submitCateringInquiry(formData: FormData) {
    const rawFormData = {
        namhello@antalyarestaurant.ca),
    email: formData.get('email'),
        phone: formData.get('phone'),
            eventType: formData.get('eventType'),
                date: formData.get('date'),
                    guests: formData.get('guests'),
                        message: formData.get('message'),
    };

try {
    // Send email to hello@antalyarestaurant.ca
    await transporter.sendMail({
        from: process.env.EMAIL_USER || '"Antalya Website" <noreply@antalyarestaurant.ca>',
        to: 'hello@antalyarestaurant.ca',
        subject: `Catering Inquiry: ${rawFormData.eventType} - ${rawFormData.name}`,
        text: `
                Name: ${rawFormData.name}
                Email: ${rawFormData.email}
                Phone: ${rawFormData.phone}
                Event Type: ${rawFormData.eventType}
                Event Date: ${rawFormData.date}
                Number of Guests: ${rawFormData.guests}
                Message: ${rawFormData.message}
            `,
        html: `
                <div style="font-family: Arial, sans-serif; line-height: 1.6; color: #441109;">
                    <h2 style="color: #c49c5c; border-bottom: 2px solid #c49c5c; padding-bottom: 10px;">New Catering Inquiry</h2>
                    <p><strong>Name:</strong> ${rawFormData.name}</p>
                    <p><strong>Email:</strong> ${rawFormData.email}</p>
                    <p><strong>Phone:</strong> ${rawFormData.phone}</p>
                    <p><strong>Event Type:</strong> ${rawFormData.eventType}</p>
                    <p><strong>Event Date:</strong> ${rawFormData.date}</p>
                    <p><strong>Number of Guests:</strong> ${rawFormData.guests}</p>
                    <div style="margin-top: 20px; padding: 15px; background: #fdfaf5; border-radius: 8px;">
                        <p><strong>Message:</strong></p>
                        <p>${rawFormData.message || 'No special requests provided.'}</p>
                    </div>
                </div>
            `,
    });

    return { success: true, message: 'Your catering inquiry has been submitted successfully to hello@antalyarestaurant.ca!' };
} catch (error) {
    console.error('Failed to send catering inquiry email:', error);
    return { success: false, message: 'There was an error sending your inquiry. Please try again or email us directly.' };
}
}
