"use server"
import { prisma } from '@/lib/prisma'
import { StringNullableChain } from 'lodash';
import nodemailer from 'nodemailer';

export const getAboutUs = async () => {
    return await prisma?.admin_editing.findUnique({ where: { id: 1 } });
}

// sends contact us to an email
// email
const transporter = nodemailer.createTransport({
    host: 'mail.mybestcall.io', // Outgoing mail server (SMTP)
    port: 465,                  // SMTP port for SSL
    secure: true,               // Use SSL
    auth: {
        user: 'donotreply@mybestcall.io',
        pass: process.env.PASS,
    },
} as nodemailer.TransportOptions);


// Function to send an email
export const sendInvitationEmail = async (data: { email: string; subject: string; message: string; reason: string; }) => {
    try {

        const htmlContent = `
        <!DOCTYPE html><html><head><title>My Best Call</title></head><body><h1>My Best Call Contact Us</h1><p> 
        subject: ${data.subject}
        <br>
        email: ${data.email}
        <br>
        reason:
        ${data.reason}
        <br>
        Message: <br>
        ${data.message}
        <br>
        </p></body></html>`;

        const info = await transporter.sendMail({
            from: '"My Best Call" <donotreply@mybestcall.io>',
            to: 'firasreggai@gmail.com',
            subject: data.subject,
            html: htmlContent as string
        });

        console.log('Email sent: %s', info.messageId);
    } catch (error) {
        console.error('Error sending email:', error);
    }
};


type Invitation = {
    firstname: string;
    lastname: string;
    email: string;
};
