import axios from "axios"
import type { AxiosResponse } from "axios"
import type { APIResponse } from "@/types"
import type { LoginDetails } from "@/types/amp"

export const ADSModule = {
	async GetInstances(host: string, SESSIONID: string) {
		return axios.post(host + "/API/ADSModule/GetInstances", { SESSIONID })
			.then((response: AxiosResponse<APIResponse>) => {
				if (!Array.isArray(response.data)) return { status: "fail", data: response.data }
				return { status: "success", data: response.data }
			})
			.catch((error: unknown) => { console.error(error); return { status: "error", data: error } })
	},
	async ManageInstance(host: string, SESSIONID: string, InstanceId: string) {
		return axios.post(host + "/API/ADSModule/ManageInstance", { SESSIONID, InstanceId })
			.then((response: AxiosResponse<APIResponse>) => {
				if (!response.data.result) return { status: "fail", data: response.data }
				return { status: "success", data: response.data }
			})
			.catch((error: unknown) => { console.error(error); return { status: "error", data: error } })
	},
	async RestartInstance(host: string, SESSIONID: string, InstanceName: string) {
		return axios.post(host + "/API/ADSModule/RestartInstance", { SESSIONID, InstanceName })
			.then((response: AxiosResponse<APIResponse>) => {
				//if (!response.data.success) return { status: "fail", data: response.data }
				return { status: "success", data: response.data }
			})
			.catch((error: unknown) => { console.error(error); return { status: "error", data: error } })
	},
	async Servers(host: string, SESSIONID: string, InstanceId: string) {
		return axios.get(host + "/API/ADSModule/Servers", { data: { SESSIONID, InstanceId } })
			.then((response: AxiosResponse<APIResponse>) => {
				if (!response.data.result) return { status: "fail", data: response.data }
				return { status: "success", data: response.data }
			})
			.catch((error: unknown) => { console.error(error); return { status: "error", data: error } })
	}
}
export const Core = {
	async Login(host: string, details: LoginDetails) {
		return axios.post(host + "/API/Core/Login", details)
			.then((response: AxiosResponse<APIResponse>) => {
				if (!response.data.success) return { status: "fail", data: response.data }
				return { status: "success", data: response.data }
			})
			.catch((error: unknown) => { console.error(error); return { status: "error", data: error } })
	}
}
export async function CheckSession(host: string, SESSIONID: string) {
	return axios.post(host + "/API/ADSModule/GetInstances", { SESSIONID })
		.then((response: AxiosResponse<APIResponse>) => {
			if (!Array.isArray(response.data)) return { status: "fail", data: response.data }
			return { status: "success", data: response.data }
		})
		.catch((error: unknown) => { console.error(error); return { status: "error", data: error } })
}
