import { Mission, Station } from "@repo/db";
import axios from "axios";

interface NtfyHeader {
	headers: {
		Title: string;
		Tags: string;
		Priority: string;
	};
}

const getLocation = (mission: Mission) => {
	return `🏡 Ort: 
    ${mission.addressStreet}, ${mission.addressZip} ${mission.addressCity}\n\n`;
};

const getCordinates = (mission: Mission) => {
	let cords = `📍 Koordinaten:        
        - Breite: ${mission.addressLat}
        - Länge: ${mission.addressLng}`;

	return `${cords}\n\n`;
};

const getInfo = (mission: Mission) => {
	return `⚕️ Info: 
    ${mission.missionKeywordAbbreviation}\n\n`;
};

const getPatientInfo = (mission: Mission) => {
	if (!mission.missionPatientInfo) return "";

	return `🧍‍♂️ Patient: 
    ${mission.missionPatientInfo}\n\n`;
};

const getAdditionalInfo = (mission: Mission) => {
	if (!mission.missionAdditionalInfo) return "";

	return `📋 Anmerkung: 
    ${mission.missionAdditionalInfo}\n\n`;
};

const getRthCallsigns = (mission: Mission, stations: Station[]) => {
	const callsigns: Array<string> = [];
	stations.forEach((station) => {
		callsigns.push(station.bosCallsignShort);
	});

	return `🚁 RTH${callsigns.length > 1 ? "s" : ""}: ${callsigns.join(" / ")} `;
};

const getNtfyHeader = (
	mission: Mission,
	clientStation: Station,
): NtfyHeader => ({
	headers: {
		Title: `${clientStation.bosCallsignShort} / ${mission.missionKeywordAbbreviation} / ${mission.missionKeywordCategory}`,
		Tags: "pager",
		Priority: "4",
	},
});

const getNtfyData = (mission: Mission, stations: Station[]): string =>
	`${new Date().toLocaleString()}\n\n` +
	`${getInfo(mission)}` +
	`${getLocation(mission)}` +
	`${getCordinates(mission)}` +
	`${getPatientInfo(mission)}` +
	`${getAdditionalInfo(mission)}` +
	`${getRthCallsigns(mission, stations)}`;

export const sendNtfyMission = async (
	mission: Mission,
	stations: Station[],
	clientStation: Station,
	ntfyRoom: string,
) => {
	axios.post(
		`https://ntfy.sh/${ntfyRoom}`,
		getNtfyData(mission, stations),
		getNtfyHeader(mission, clientStation),
	);
};
