import axios from 'axios';

async function getCSRF() {
    let response = await axios({
        url: "/users/csrf/",
        method: "GET",
        withCredentials: true
    })
    let csrftoken = response.headers["x-csrftoken"]
    return csrftoken
}

export async function login({ username, password }) {
    try {
        let csrfToken = await getCSRF()
        let response = await axios({
            url: "/users/login/",
            method: "POST",
            headers: {
                "Content-Type": "application/json",
                "X-CSRFToken": csrfToken,
            },
            withCredentials: true,
            data: { username: username, password: password },
        })
        if (response.status == 200) {
            return true
        } else {
            return false
        }
    } catch (e) {
        console.log(e.message)
        return false
    }

}

export async function getUser() {
    try {
        let csrfToken = await getCSRF()
        let response = await axios({
            url: "/users/me/",
            method: "GET",
            withCredentials: true,
            headers: {
                "X-CSRFToken": csrfToken,
            },
        })
        if (response.status == 200 && response.data.user) {
            return response.data.user
        }
    } catch (e) {
        return null
    }

}

export async function logout() {
    let csrfToken = await getCSRF()
    let response = await axios({
        url: "/users/logout/",
        method: "POST",
        headers: {
            "X-CSRFToken": csrfToken,
        },
        withCredentials: true,
    })
    if (response.status == 200) {
        return true
    } else {
        return false
    }
}

export async function createPost(params) {
    let csrfToken = await getCSRF()
    let response = await axios({
        url: "/posts/",
        method: "POST",
        headers: {
            "Content-Type": "application/json",
            "X-CSRFToken": csrfToken,
        },
        withCredentials: true,
        data: params
    })
    if (response.status == 200) {
        return true
    }
}


export async function findUser(id) {
    let csrfToken = await getCSRF()
    let response = await axios({
        url: `/users/${id}`,
        method: "GET",
        headers: {
            "X-CSRFToken": csrfToken,
        },
        withCredentials: true,
    })
    if (response.status == 200) {
        return response.data
    }
}

export async function registerUser(data) {
    try{
        let csrfToken = await getCSRF()
        let response = await axios({
            url: `/users/register/`,
            method: "POST",
            headers: {
                "Content-Type": "application/json",
                "X-CSRFToken": csrfToken,
            },
            withCredentials: true,
            data: data
        })
        if (response.status == 500) {
            return true
        }
        return false
    }catch(e) {
        console.log(e.message)
        return false
    }
}