import { toast } from "sonner";
import { ApiClientError } from "@/lib/frontend/apiClient";

function normalizeText(value) {
	if (typeof value !== "string") return null;
	const s = value.trim();
	return s ? s : null;
}

function buildToastOptions({ description, ...rest }) {
	const opts = { ...rest };

	const desc = normalizeText(description);
	if (desc) opts.description = desc;

	return opts;
}

export function notifySuccess({ title, description, ...options } = {}) {
	const t = normalizeText(title);
	if (!t) return null;

	return toast.success(t, buildToastOptions({ description, ...options }));
}

export function notifyError({ title, description, ...options } = {}) {
	const t = normalizeText(title);
	if (!t) return null;

	return toast.error(t, buildToastOptions({ description, ...options }));
}

export function notifyInfo({ title, description, ...options } = {}) {
	const t = normalizeText(title);
	if (!t) return null;

	return toast.info(t, buildToastOptions({ description, ...options }));
}

export function notifyWarning({ title, description, ...options } = {}) {
	const t = normalizeText(title);
	if (!t) return null;

	return toast.warning(t, buildToastOptions({ description, ...options }));
}

export function notifyLoading({ title, description, ...options } = {}) {
	const t = normalizeText(title);
	if (!t) return null;

	return toast.loading(t, buildToastOptions({ description, ...options }));
}

export function dismissToast(toastId) {
	return toast.dismiss(toastId);
}

export function mapApiErrorToToast(
	err,
	{
		overrides = null,
		fallbackTitle = "Fehler",
		fallbackDescription = "Bitte versuchen Sie es erneut.",
	} = {},
) {
	// Allow call sites (like Change Password) to override messages per error code.
	if (err instanceof ApiClientError) {
		const code = String(err.code || "");

		if (overrides && overrides[code]) {
			const o = overrides[code] || {};
			return {
				title: normalizeText(o.title) || fallbackTitle,
				description: normalizeText(o.description) || null,
			};
		}

		if (code === "CLIENT_NETWORK_ERROR") {
			return {
				title: "Netzwerkfehler",
				description:
					"Bitte prüfen Sie Ihre Verbindung und versuchen Sie es erneut.",
			};
		}

		if (code === "AUTH_UNAUTHENTICATED") {
			return {
				title: "Sitzung abgelaufen",
				description: "Bitte melden Sie sich erneut an.",
			};
		}

		if (code.startsWith("VALIDATION_")) {
			return {
				title: "Ungültige Eingabe",
				description:
					"Bitte prüfen Sie Ihre Eingaben und versuchen Sie es erneut.",
			};
		}
	}

	return {
		title: normalizeText(fallbackTitle) || "Fehler",
		description: normalizeText(fallbackDescription) || null,
	};
}

export function notifyApiError(
	err,
	{
		overrides = null,
		fallbackTitle = "Fehler",
		fallbackDescription = "Bitte versuchen Sie es erneut.",
		...toastOptions
	} = {},
) {
	const mapped = mapApiErrorToToast(err, {
		overrides,
		fallbackTitle,
		fallbackDescription,
	});

	return toast.error(
		mapped.title,
		buildToastOptions({ description: mapped.description, ...toastOptions }),
	);
}
