import axios from "axios";
import dayjs from "dayjs";

const baseURL = /*"http://localhost:3333"*/ "https://functions.fedeo.io"

export const useFunctions = () => {
    const supabase = useSupabaseClient()

    const getWorkingTimesEvaluationData = async (profileId, startDate, endDate) => {
        const {data:{session:{access_token}}} = await supabase.auth.getSession()

        return (await axios({
            method: "POST",
            url: `${baseURL}/functions/workingtimeevaluation`,
            data: {
                profile: profileId,
                startDate: dayjs(startDate).format("YYYY-MM-DD"),
                endDate: dayjs(endDate).format("YYYY-MM-DD"),
            },
            headers: {
                Authorization: `Bearer ${access_token}`
            }
        })).data
    }

    const useNextNumber = async (numberRange) => {
        const {data:{session:{access_token}}} = await supabase.auth.getSession()

        return (await axios({
            method: "POST",
            url: `${baseURL}/functions/usenextnumber`,
            data: {
                numberRange: numberRange,
            },
            headers: {
                Authorization: `Bearer ${access_token}`
            }
        })).data.usedNumber
    }

    const useCreateTicket = async (subject,message,url,source) => {
        const {data:{session:{access_token}}} = await supabase.auth.getSession()

        const {data} = await axios({
            method: "POST",
            url: `${baseURL}/functions/createticket`,
            data: {
                subject,
                message,
                source,
                url
            },
            headers: {
                Authorization: `Bearer ${access_token}`
            }
        })

        return !!data.ticket_created;

    }

    const useBankingGenerateLink = async (institutionId) => {
        const {data:{session:{access_token}}} = await supabase.auth.getSession()

        const {data} = await axios({
            method: "POST",
            url: `${baseURL}/functions/bankstatements/generatelink`,
            data: {
                institutionId
            },
            headers: {
                Authorization: `Bearer ${access_token}`
            }
        })

        console.log(data)

        return data.link

    }

    const useCreatePDF = async (invoiceData,path) => {
        const {data:{session:{access_token}}} = await supabase.auth.getSession()

        const {data} = await axios({
            method: "POST",
            url: `${baseURL}/functions/createpdf`,
            data: {
                invoiceData: invoiceData,
                backgroundPath: path,
                returnMode: "base64"
            },
            headers: {
                Authorization: `Bearer ${access_token}`
            }
        })

        console.log(data)

        return `data:${data.mimeType};base64,${data.base64}`

    }

    const useSendTelegramNotification = async (message) => {
        const {data:{session:{access_token}}} = await supabase.auth.getSession()

        const {data,error} = await axios({
            method: "POST",
            url: `${baseURL}/functions/sendtelegramnotification`,
            data: {
                message: message
            },
            headers: {
                Authorization: `Bearer ${access_token}`
            }
        })

        if(error){

        } else {
            return true
        }
    }

    const useBankingCheckInstitutions = async (bic) => {
        const {data:{session:{access_token}}} = await supabase.auth.getSession()

        const {data} = await axios({
            method: "GET",
            url: `${baseURL}/functions/bankstatements/checkinstitutions/${bic}`,
            headers: {
                Authorization: `Bearer ${access_token}`
            }
        })

        return data

    }

    const useBankingListRequisitions = async (reqId) => {
        const {data:{session:{access_token}}} = await supabase.auth.getSession()

        const {data} = await axios({
            method: "GET",
            url: `${baseURL}/functions/bankstatements/listrequisitions/${reqId}`,
            headers: {
                Authorization: `Bearer ${access_token}`
            }
        })

        return data

    }

    return {getWorkingTimesEvaluationData, useNextNumber, useCreateTicket, useBankingGenerateLink, useBankingCheckInstitutions, useBankingListRequisitions, useCreatePDF, useSendTelegramNotification}
}