import { useAuthStore } from "@/store/authStore";

const API_URL = "https://hopp.zikor.pl/api/v1";

export const sendEmail = async (emailData) => {
    const token = useAuthStore.getState().token;

    try {
        const response = await fetch(`${API_URL}/email/send`, {
            method: "POST",
            headers: {
                "Content-Type": "application/json",
                ...(token && { Authorization: `Bearer ${token}` }),
            },
            body: JSON.stringify(emailData),
        });

        if (!response.ok) {
            const errorMessage = `HTTP error! Status: ${response.status}`;
            console.error("Error sending email:", errorMessage);
            return { success: false, error: errorMessage };
        }

        const result = await response.text();
        return { success: true, result };
    } catch (error) {
        console.error("Error sending email:", error.message);
        return { success: false, error: error.message };
    }
};