import axios from "axios";
import { fingerPrint } from "../utils/fingerPrint";
import { CreateTokenProps, ICreateTokenSucces } from "../interfaces/services/ICreateToken";
import { RecoverPasswordProps, IRecoverPasswordSucces } from "../interfaces/services/IRecoverPassword";
import { CreateTokenSocialProps, ICreateTokenSocialSucces } from "../interfaces/services/ICreateTokenSocial";
import { ValidarDadosUsuarioProps } from "../interfaces/services/IValidarEmail";
import { GetCidadesProps, PayloadCidades } from "../interfaces/services/ICidades";
import { CreateAccountProps } from "../interfaces/services/ICreateUser";
import { TipoUsuarioProps } from "../interfaces/services/ITipoUsuario";
import { IUsuarioPreConta, UsuarioPreContaProps } from "../interfaces/services/IUsuarioPreConta";
export const isOnline = (apiUrl:string) => {
    return axios.head(`${apiUrl}/api/baratheon/v1`, {})
}

export const createToken = async ({ apiUrl, usuario, senha, empreendimento, plataforma, latitude, longitude }:CreateTokenProps):Promise<ICreateTokenSucces> => {
	const bolton = fingerPrint();

    const url = `${apiUrl}/api/baratheon/v1/empreendimentos/${empreendimento}/${plataforma}/token`;

    const { data } = await axios.post(url, {
        usuario,
        senha,
        latitude,
		longitude,

	}, {
        headers: {
			bolton
		}
    })

    return data;
}

export const createTokenSocial = async ({apiUrl, empreendimento, plataforma, body}:CreateTokenSocialProps):Promise<ICreateTokenSocialSucces> =>{
    const url = `${apiUrl}/api/baratheon/v1/empreendimentos/${empreendimento}/plataforma/${plataforma}/social/token`;
	const bolton = fingerPrint();

    const { data } = await axios.post(url, body, {
        headers: {
			bolton
		}
    })

    return data;

}

export const recoverPassword = async ({apiUrl, email}:RecoverPasswordProps):Promise<IRecoverPasswordSucces> => {
    const url = `${apiUrl}/api/baratheon/v1/usuarios/password`;

    const { data } = await axios.post(url, {
		redirect: btoa(window.location.href),
		usuario: email,
	})

    return data;
}

export const validarDadosUsuario = async ({ apiUrl, email, telefone, usuario, cpf, identificacao }:ValidarDadosUsuarioProps) =>{
    const queryParams = {
        email,
        telefone,
        usuario,
        cpf,
        identificacao
    }

    const url = Object.entries(queryParams).reduce((url, [key, value]) =>{
        if(value){
            url = url + `${key}=${value}`
        }
        return url
    }, `${apiUrl}/api/baratheon/v1/usuarios?`)

    const { data } = await axios.head(url)

    return data;
}

export const getCidades = async ({apiUrl, cidade}: GetCidadesProps):Promise<PayloadCidades> =>{
	const url = `${apiUrl}/api/baratheon/v1/cidades?cidade=${cidade}`;

    const { data } = await axios.get(url)
    return data;
}

export const createAccount = async ({ apiUrl, body }:CreateAccountProps) =>{
    const url = `${apiUrl}/api/baratheon/v1/usuarios`;

    const { data } = await axios.post(url, body)
    return data;
}

export const tipoUsuario = async ({apiUrl, usuario}:TipoUsuarioProps ) => {
    const url = `${apiUrl}/api/baratheon/v1/contas?identificador=${usuario}`;

    const { data } = await axios.head(url)
    return data;

}

export const usuarioPreconta = async ({apiUrl, usuario}:UsuarioPreContaProps):Promise<IUsuarioPreConta> => {
    const url = `${apiUrl}/api/baratheon/v1/usuarios/preconta?identificador=${usuario}`;
    const { data } = await axios.get(url)

    return data;
}