import axios from "@/config/axios";
import axios2 from "axios";

export const createPengunjung = async () => {
	try {
		const jsonip = await axios2.get("https://ip-api.io/json");
		const res = await axios.post("/pengunjung/create", {
			os: navigator.userAgentData.platform,
			ipv4: jsonip.data.ip,
			location: {
				country: jsonip.data.country_name,
				region: jsonip.data.region_name,
				city: jsonip.data.city,
				lat: jsonip.data.latitude,
				lon: jsonip.data.longitude,
				timezone: jsonip.data.time_zone,
			},
		});
		return res.data;
	} catch (error) {
		console.log("error", error);
		return false;
	}
};

export const getPengunjung = async (token) => {
	try {
		const res = await axios.get(`/pengunjung`, { headers: { Authorization: token } });
		return res.data;
	} catch (error) {
		console.log("error", error);
		return false;
	}
};

export const getPengunjungPublic = async ({ bulan, tahun }) => {
	try {
		let url = "/public/pengunjung";
		if (bulan || tahun) {
			url += "?";
			if (bulan) {
				url += `bulan=${bulan}`;
			}
			if (tahun) {
				url += `bulan=${tahun}`;
			}
		}
		const res = await axios.get(url);
		return res.data;
	} catch (error) {
		console.log("error", error);
		return false;
	}
};
