import type { CompanyId } from "@/core/auth/types";

// ── Notification types ──

export type NotificationType =
  | "deadline-urgent"
  | "deadline-overdue"
  | "document-expiry"
  | "status-change";

export interface NotificationTypeInfo {
  type: NotificationType;
  label: string;
  description: string;
}

/** All notification types with Romanian labels */
export const NOTIFICATION_TYPES: NotificationTypeInfo[] = [
  {
    type: "deadline-urgent",
    label: "Termene urgente",
    description: "Termene legale cu 5 sau mai putine zile ramase",
  },
  {
    type: "deadline-overdue",
    label: "Termene depasite",
    description: "Termene legale care au depasit data scadenta",
  },
  {
    type: "document-expiry",
    label: "Documente care expira",
    description: "CU/AC si alte documente care expira in fereastra de alerta",
  },
  {
    type: "status-change",
    label: "Schimbare status extern",
    description: "Notificare imediata cand o autoritate actualizeaza statusul documentului",
  },
];

// ── User preferences ──

export interface NotificationPreference {
  userId: string;
  email: string;
  name: string;
  company: CompanyId;
  enabledTypes: NotificationType[];
  globalOptOut: boolean;
}

/** Default preference — all types enabled, not opted out */
export function defaultPreference(
  userId: string,
  email: string,
  name: string,
  company: CompanyId,
): NotificationPreference {
  return {
    userId,
    email,
    name,
    company,
    enabledTypes: ["deadline-urgent", "deadline-overdue", "document-expiry", "status-change"],
    globalOptOut: false,
  };
}

// ── Digest result types ──

export interface DigestItem {
  /** Entry number (e.g., "B-2026-00042") */
  entryNumber: string;
  /** Entry subject */
  subject: string;
  /** Deadline or expiry label */
  label: string;
  /** Due date (YYYY-MM-DD) */
  dueDate: string;
  /** Days remaining (negative = overdue) */
  daysRemaining: number;
  /** Color for HTML rendering */
  color: "red" | "yellow" | "blue";
}

export interface DigestSection {
  type: NotificationType;
  title: string;
  items: DigestItem[];
}

export interface DigestResult {
  success: boolean;
  totalEmails: number;
  errors: string[];
  companySummary: Record<string, { emails: number; sections: number }>;
}

// ── Email payload ──

export interface EmailPayload {
  to: string;
  subject: string;
  html: string;
}
