/**
 * Dashboard widgets and per-industry default layouts. Each member can reorder or hide widgets (memberships.dashboard_layout);
 * "Reset" returns to the industry default.
 */
import { normalizeIndustry } from "./industry";

export type WidgetId =
  | "pipeline_value" | "hero" | "kpis" | "sales_overview" | "sales_target" | "recent_deals" | "industry_focus"
  | "trend" | "funnel" | "tasks" | "new_leads" | "alerts" | "inbox";

export const WIDGETS: Record<WidgetId, { title: string; description: string; span: string }> = {
  pipeline_value: { title: "Pipeline value", description: "Open deal value with the biggest stages and quick actions", span: "lg:col-span-4" },
  hero: { title: "Revenue won", description: "Won revenue for the period vs the previous period", span: "lg:col-span-4" },
  kpis: { title: "Key numbers", description: "New leads, open deals, win rate and first response", span: "lg:col-span-4" },
  sales_overview: { title: "Sales overview", description: "Won vs lost deals by month", span: "lg:col-span-8" },
  sales_target: { title: "Quarterly target", description: "Progress against your quarterly revenue target", span: "lg:col-span-4" },
  recent_deals: { title: "Recent deals", description: "Latest deal activity with search", span: "lg:col-span-8" },
  industry_focus: { title: "Industry focus", description: "The stages and bookings that matter most in your industry", span: "lg:col-span-4" },
  trend: { title: "Leads, spend & sales", description: "Daily leads against ad spend and wins", span: "lg:col-span-8" },
  funnel: { title: "Funnel", description: "Conversion and cost per stage", span: "lg:col-span-4" },
  tasks: { title: "Due today", description: "Your tasks and follow-ups", span: "lg:col-span-4" },
  new_leads: { title: "Newest leads", description: "The latest people to enquire", span: "lg:col-span-4" },
  alerts: { title: "Alerts", description: "Anomalies and budget pacing", span: "lg:col-span-4" },
  inbox: { title: "Inbox", description: "Open, unanswered and overdue conversations", span: "lg:col-span-4" },
};

const BASE: WidgetId[] = ["pipeline_value", "hero", "kpis", "sales_overview", "sales_target", "recent_deals", "industry_focus", "trend", "funnel", "tasks", "new_leads", "alerts", "inbox"];

/** Default order (and hidden widgets) by industry: B2B leads with pipeline & targets, B2C service with bookings & inbox, retail with traffic & conversations. */
const BY_INDUSTRY: Record<string, { order: WidgetId[]; hidden: WidgetId[] }> = {
  healthcare: { order: ["industry_focus", "kpis", "hero", "inbox", "tasks", "sales_overview", "new_leads", "funnel", "recent_deals", "trend", "alerts", "pipeline_value", "sales_target"], hidden: ["sales_target"] },
  financial_services: { order: ["industry_focus", "kpis", "pipeline_value", "hero", "funnel", "recent_deals", "tasks", "sales_overview", "inbox", "alerts", "trend", "new_leads", "sales_target"], hidden: [] },
  pharma_meddevices: { order: ["pipeline_value", "industry_focus", "hero", "kpis", "recent_deals", "sales_target", "sales_overview", "funnel", "tasks", "alerts", "inbox", "trend", "new_leads"], hidden: [] },
  retail_ecommerce: { order: ["hero", "kpis", "inbox", "trend", "sales_overview", "new_leads", "industry_focus", "funnel", "alerts", "recent_deals", "tasks", "pipeline_value", "sales_target"], hidden: ["sales_target"] },
  media_entertainment: { order: ["hero", "kpis", "trend", "inbox", "sales_overview", "industry_focus", "new_leads", "funnel", "alerts", "recent_deals", "tasks", "pipeline_value", "sales_target"], hidden: ["sales_target"] },
  travel_hospitality: { order: ["kpis", "hero", "industry_focus", "inbox", "trend", "sales_overview", "new_leads", "tasks", "funnel", "recent_deals", "alerts", "pipeline_value", "sales_target"], hidden: [] },
  real_estate: { order: ["pipeline_value", "hero", "kpis", "sales_overview", "industry_focus", "recent_deals", "trend", "funnel", "sales_target", "tasks", "new_leads", "alerts", "inbox"], hidden: [] },
};
const B2B = ["it_martech", "professional_services", "property_facility", "transport_logistics", "telecom", "consumer_electronics", "automotive", "electrical_industrial", "agriculture_food", "energy_utilities"];

export function defaultLayout(industry?: string | null): { order: WidgetId[]; hidden: WidgetId[] } {
  const key = normalizeIndustry(industry);
  if (BY_INDUSTRY[key]) return BY_INDUSTRY[key]!;
  if (B2B.includes(key)) return { order: ["pipeline_value", "hero", "kpis", "sales_target", "recent_deals", "sales_overview", "industry_focus", "funnel", "tasks", "trend", "new_leads", "alerts", "inbox"], hidden: [] };
  return { order: BASE, hidden: [] };
}

/** Saved layout merged with the registry: unknown ids dropped, new widgets appended so upgrades never hide them. */
export function resolveLayout(saved: { order?: string[]; hidden?: string[] } | null | undefined, industry?: string | null) {
  const def = defaultLayout(industry);
  if (!saved?.order?.length) return def;
  const valid = saved.order.filter((id): id is WidgetId => id in WIDGETS);
  const missing = def.order.filter((id) => !valid.includes(id));
  return { order: [...valid, ...missing], hidden: (saved.hidden ?? []).filter((id): id is WidgetId => id in WIDGETS) };
}
