import getXSRFToken from "../getXSRF.js";

export default async function login(email, password) {
    const csrfToken = await getXSRFToken();

    try {
        const response = await fetch(`http://${import.meta.env.VITE_API_URL}/api/users/login`, {
            method: 'POST',
            credentials: 'include',
            headers: {
                'Content-Type': 'application/json',
                'X-XSRF-TOKEN': csrfToken,
                'Accept': 'application/json'
            },
            body: JSON.stringify({ email, password })
        });

        const data = await response.json();

        if (!response.ok) {
            return {
                status: response.status,
                errors: data.errors || data.message
            };
        }

        return { status: response.status, data };

    } catch (error) {
        return { status: 500, error };
    }
}
