import type { NotificationItem } from "@shared/store/notifications"
import type { BadgeVariant } from "@shared/ui/Badge/Badge.d"

const JOB_TYPE_LABELS: Record<string, string> = {
	MEDIA_PROBE: "Анализ медиа",
	SILENCE_REMOVE: "Удаление тишины",
	SILENCE_DETECT: "Анализ тишины",
	SILENCE_APPLY: "Применение вырезок",
	MEDIA_CONVERT: "Конвертация",
	TRANSCRIPTION_GENERATE: "Транскрипция",
	CAPTIONS_GENERATE: "Генерация субтитров",
	FRAME_EXTRACT: "Извлечение кадров",
}

const STATUS_LABELS: Record<string, string> = {
	PENDING: "Ожидание",
	RUNNING: "Выполняется",
	DONE: "Завершено",
	FAILED: "Ошибка",
}

const STATUS_VARIANTS: Record<string, BadgeVariant> = {
	PENDING: "secondary",
	RUNNING: "info",
	DONE: "success",
	FAILED: "danger",
}

export interface NotificationPresentation {
	title: string
	statusText: string | null
	statusVariant: BadgeVariant | null
	detailText: string | null
}

function normalizeText(value: string | null | undefined): string | null {
	const trimmed = value?.trim()
	return trimmed ? trimmed : null
}

export function getNotificationPresentation(
	item: NotificationItem,
): NotificationPresentation {
	const statusText = item.status ? (STATUS_LABELS[item.status] ?? item.status) : null
	const statusVariant = item.status ? (STATUS_VARIANTS[item.status] ?? null) : null
	const rawTitle = normalizeText(item.title)
	const rawMessage = normalizeText(item.message)
	const title = item.job_type
		? (JOB_TYPE_LABELS[item.job_type] ?? rawTitle ?? "Уведомление")
		: (rawTitle ?? "Уведомление")

	return {
		title,
		statusText,
		statusVariant,
		detailText:
			rawMessage && rawMessage !== statusText && rawMessage !== rawTitle
				? rawMessage
				: null,
	}
}
