import axios from "axios";

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

export async function login(userData) {
    try {
        const response = await axios.post(`${API_URL}/auth/login`, userData, {
            headers: {"Content-Type": "application/json"},
        });
        return response.data;
    } catch (error) {
        console.error("Login failed:", error);
        throw error.response?.data?.message || "Login failed";
    }
}

export async function register(userData) {
    try {
        const response = await axios.post(`${API_URL}/auth/register`, userData, {
            headers: {"Content-Type": "application/json"},
        });

        return response.data;
    } catch (error) {
        console.error("Registration failed:", error);
        throw error.response?.data?.message || "Registration failed";
    }
}

export async function googleLogin(googleToken) {
    try {
        const response = await axios.post(
            `${API_URL}/auth/google`,
            { googleToken: googleToken },
            {
                headers: { "Content-Type": "application/json" },
            }
        );

        return response.data;
    } catch (error) {
        console.error("Google login failed:", error);
        throw error.response?.data?.message || "Google login failed";
    }
}

export async function logout(token) {
    const headers = token ? { Authorization: `Bearer ${token}` } : {};
    try {
        const response = await axios.post(
            `${API_URL}/auth/logout`,
            {},
            {
                headers: headers,
            }
        );

        return response.data;
    } catch (error) {
        console.error("Logout failed:", error);
        throw error.response?.data?.message || "Logout failed";
    }
}