import axios from "axios";

export async function submitReservation(formData: FormData) {
    const name = formData.get('name');
    const phone = formData.get('phone');
    const email = formData.get('email'); // Added if available
    const date = formData.get('date');
    const message = formData.get('message');
    const guests = formData.get('guests') || "2 Guests";

    const emailData = {
        name: name,
        phone: phone,
        email: email || "No Email Provided",
        subject: `Table Reservation - ${guests} on ${date}`,
        message: `
            <strong>Reservation Details:</strong><br/>
            Name: ${name}<br/>
            Phone: ${phone}<br/>
            Guests: ${guests}<br/>
            Date: ${date}<br/><br/>
            <strong>Special Requests:</strong><br/>
            ${message || "None"}
        `,
        to: "hello@antalyarestaurant.ca",
        senderName: "Antalya Restaurant - Table Reservation",
    };

    try {
        await axios.post(
            "https://mailserver.metatronnest.com/send",
            emailData,
            { headers: { "Content-Type": "application/json" } }
        );
        return { success: true, message: 'Reservation submitted successfully!' };
    } catch (error) {
        console.error('Failed to submit reservation:', error);
        return { success: false, message: 'Failed to submit reservation. Please try again later.' };
    }
}

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

    const emailData = {
        name: rawFormData.name,
        email: rawFormData.email,
        phone: rawFormData.phone,
        subject: `Catering Inquiry: ${rawFormData.eventType} - ${rawFormData.name}`,
        message: `
            <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>
        `,
        to: 'hello@antalyarestaurant.ca',
        senderName: "Antalya Restaurant - Catering Inquiry",
    };

    try {
        await axios.post(
            "https://mailserver.metatronnest.com/send",
            emailData,
            { headers: { "Content-Type": "application/json" } }
        );
        return { success: true, message: 'Your catering inquiry has been submitted successfully to hello@antalyarestaurant.ca!' };
    } catch (error) {
        console.error('Failed to send catering inquiry:', error);
        return { success: false, message: 'There was an error sending your inquiry. Please try again or email us directly.' };
    }
}
