import axios from "axios"

const AxiosEE = axios.create({
    baseURL: process.env.EMAILENGINE_URL ||"https://ee.fedeo.io/v1",
    headers: {
        Authorization: `Bearer ${process.env.EMAILENGINE_TOKEN || "dcd8209bc5371c728f9ec951600afcfc74e8c391a7e984b2a6df9c4665dc7ad6"}`,
        Accept: "application/json",
    },
})



export async function sendMailAsUser(
    to: string,
    subject: string,
    html: string,
    text:  string,
    account: string,
    cc: string,
    bcc: string,
    attachments: any,
): Promise<{ success: boolean; info?: any; error?: any }> {
    try {
        const sendData = {
            to: to.split(";").map(i => { return {address: i}}),
            cc: cc ? cc.split(";").map((i:any) => { return {address: i}}) : null,
            bcc: bcc ? bcc.split(";").map((i:any) => { return {address: i}}) : null,
            subject,
            text,
            html,
            attachments
        }

        if(sendData.cc === null) delete sendData.cc
        if(sendData.bcc === null) delete sendData.bcc

        const {data} = await AxiosEE.post(`/account/${account}/submit`, sendData)

        return { success: true, info: data }

    } catch (err) {
        console.error("❌ Fehler beim Mailversand:", err)
        return { success: false, error: err }
    }
}