import { Mission, MissionSdsLog, Prisma } from "@repo/db";
import axios from "axios";
import { serverApi } from "_helpers/axios";

export const getMissionsAPI = async (
	filter?: Prisma.MissionWhereInput,
	include?: Prisma.MissionInclude,
	orderBy?: Prisma.MissionOrderByWithRelationInput,
) => {
	const res = await axios.get<Mission[]>("/api/missions", {
		params: {
			filter: JSON.stringify(filter),
			include: JSON.stringify(include),
			orderBy: JSON.stringify(orderBy),
		},
	});
	if (res.status !== 200) {
		throw new Error("Failed to fetch stations");
	}
	return res.data;
};

export const createMissionAPI = async (mission: Prisma.MissionCreateInput) => {
	const response = await serverApi.put<Mission>("/mission", mission);
	return response.data;
};

export const editMissionAPI = async (id: number, mission: Prisma.MissionUpdateInput) => {
	const respone = await serverApi.patch<Mission>(`/mission/${id}`, mission);
	return respone.data;
};
export const sendSdsMessageAPI = async ({
	missionId,
	sdsMessage,
}: {
	missionId?: number;
	sdsMessage: MissionSdsLog;
}) => {
	const respone = await serverApi.post<Mission>(`/mission/send-sds`, { sdsMessage, missionId });
	return respone.data;
};

export const startHpgValidation = async (
	id: number,
	config?: {
		alertWhenValid?: boolean;
	},
) => {
	const respone = await serverApi.post<Mission>(`/mission/${id}/validate-hpg`, config);
	return respone.data;
};

export const sendMissionAPI = async (
	id: number,
	{
		stationId,
		vehicleName,
	}: {
		stationId?: number;
		vehicleName?: "ambulance" | "police" | "firebrigade";
	},
) => {
	const respone = await serverApi.post<{
		message: string;
	}>(`/mission/${id}/send-alert`, {
		stationId,
		vehicleName,
	});
	return respone.data;
};

export const deleteMissionAPI = async (id: number) => {
	await serverApi.delete(`/mission/${id}`);
};
