import type {
  AnnouncementAudience,
  AnnouncementDisplayState,
  AnnouncementFormat,
  AnnouncementNotify,
  AnnouncementPlacement,
  AnnouncementReminder,
  AnnouncementRules,
  AnnouncementWidgetCorner,
} from "@/domain/entities/announcement";

/**
 * Feature announcements: what each format shows in the editor, as data (spec §3, §4.2, §5).
 * The editor reads FORMAT_FIELDS instead of scattering `if (format === …)` checks.
 */

export const ANNOUNCEMENT_FORMATS: AnnouncementFormat[] = [
  "badge",
  "sidebar_spotlight",
  "banner",
  "modal",
  "page_widget",
  "tour",
];

export type FieldPresence = "none" | "optional" | "required";

export interface FormatFieldSpec {
  /** Page picker. */
  page: FieldPresence;
  /** "Toutes les pages" allowed (empty page). */
  allPages: boolean;
  /** Element picker for the announcement itself: any element, menu entries only, or none. */
  element: "none" | "any" | "menu";
  /** Popover position choices; empty = fixed position, nothing to choose. */
  placements: AnnouncementPlacement[];
  /** Card corner choice (page_widget). */
  corner: boolean;
  /** Steps, with the element rule per step. */
  steps: { min: number; max: number; anchor: FieldPresence } | null;
  /** Main text and its length limit (0 = no text field). */
  bodyMax: number;
  badgeText: boolean;
  image: boolean;
  cta: boolean;
  /** Background colour picker. */
  background?: boolean;
}

export const FORMAT_FIELDS: Record<AnnouncementFormat, FormatFieldSpec> = {
  badge: {
    page: "optional", allPages: false, element: "any", placements: [], corner: false,
    steps: null, bodyMax: 0, badgeText: true, image: false, cta: false,
  },
  sidebar_spotlight: {
    page: "none", allPages: false, element: "menu", placements: ["right", "bottom"], corner: false,
    steps: null, bodyMax: 200, badgeText: true, image: false, cta: true,
  },
  banner: {
    page: "optional", allPages: true, element: "none", placements: [], corner: false,
    steps: null, bodyMax: 160, badgeText: true, image: false, cta: true, background: true,
  },
  modal: {
    page: "optional", allPages: true, element: "none", placements: [], corner: false,
    steps: null, bodyMax: 400, badgeText: true, image: true, cta: true,
  },
  page_widget: {
    page: "required", allPages: false, element: "none", placements: [], corner: true,
    steps: { min: 2, max: 4, anchor: "optional" }, bodyMax: 0, badgeText: false, image: false, cta: false,
  },
  tour: {
    page: "required", allPages: false, element: "none", placements: [], corner: false,
    steps: { min: 1, max: 8, anchor: "required" }, bodyMax: 0, badgeText: false, image: false, cta: false,
  },
};

/** Positions offered on a step that points at an element. */
export const STEP_PLACEMENTS: AnnouncementPlacement[] = ["auto", "top", "right", "bottom", "left"];

export const WIDGET_CORNERS: AnnouncementWidgetCorner[] = ["bottom_right", "bottom_left"];

export const ANNOUNCEMENT_LIMITS = {
  title: 60,
  badgeText: 12,
  stepTitle: 60,
  stepSubtitle: 80,
  stepBody: 400,
  ctaLabel: 30,
  imageMaxBytes: 2 * 1024 * 1024,
  imageTypes: ["image/jpeg", "image/png", "image/webp"],
  imageMinWidth: 800,
  maxImpressions: 20,
  priorityMax: 10,
} as const;

/** The fixed icon list (no upload). Names of lucide icons, rendered by AnnouncementIcon. */
export const ANNOUNCEMENT_ICONS = [
  "Sparkles", "Rocket", "Gift", "Star", "Lightbulb", "Zap", "Bell", "Megaphone",
  "Map", "MapPin", "Car", "Fuel", "Wrench", "ShieldCheck", "FileText", "BarChart3",
  "Users", "Calendar", "Bot", "Leaf", "Construction",
] as const;

export type AnnouncementIconName = (typeof ANNOUNCEMENT_ICONS)[number];

export const ANNOUNCEMENT_BACKGROUNDS = ["#FEF3C7", "#FFEDD5", "#FEE2E2", "#DBEAFE", "#DCFCE7", "#1F2937"] as const;

export const DEFAULT_AUDIENCE: AnnouncementAudience = {
  plans: [],
  capabilities_required: [],
  capabilities_missing: [],
  permissions: [],
  roles: [],
  companies_include: [],
  companies_exclude: [],
  languages: [],
  new_users_only: false,
  everyone_confirmed: false,
};

export const DEFAULT_RULES: AnnouncementRules = {
  dismissible: true,
  max_impressions: 3,
  hide_after_page_visit: false,
  account_age_min_days: null,
  account_age_max_days: null,
};

export const DEFAULT_REMINDER: AnnouncementReminder = {
  enabled: false,
  after_days: 7,
  format: "banner",
  max_reminders: 2,
};

export const DEFAULT_NOTIFY: AnnouncementNotify = {
  enabled: false,
  channels: ["in_app"],
  message: { fr: "", en: "" },
};

/** "New users" = accounts younger than this many days. */
export const NEW_USER_MAX_DAYS = 30;

export const DISPLAY_STATE_CLASSES: Record<AnnouncementDisplayState, string> = {
  draft: "bg-slate-100 text-slate-700 border-slate-200",
  scheduled: "bg-blue-50 text-blue-700 border-blue-200",
  live: "bg-emerald-50 text-emerald-700 border-emerald-200",
  ended: "bg-amber-50 text-amber-700 border-amber-200",
  archived: "bg-zinc-100 text-zinc-500 border-zinc-200",
};

export const FORMAT_CLASSES: Record<AnnouncementFormat, string> = {
  badge: "bg-violet-50 text-violet-700 border-violet-200",
  sidebar_spotlight: "bg-sky-50 text-sky-700 border-sky-200",
  banner: "bg-teal-50 text-teal-700 border-teal-200",
  modal: "bg-rose-50 text-rose-700 border-rose-200",
  page_widget: "bg-orange-50 text-orange-700 border-orange-200",
  tour: "bg-indigo-50 text-indigo-700 border-indigo-200",
};

/** Lucide icon name of each format (cards, badges). */
export const FORMAT_ICONS: Record<AnnouncementFormat, AnnouncementIconName | "Tag" | "PanelLeft" | "PanelTop" | "SquareStack" | "MousePointerClick" | "AppWindow"> = {
  badge: "Tag",
  sidebar_spotlight: "PanelLeft",
  banner: "PanelTop",
  modal: "AppWindow",
  page_widget: "SquareStack",
  tour: "MousePointerClick",
};
