/**
 * Widget Registry — defines all available widgets per customizable page
 * and built-in preset layouts.
 */

export type WidgetSize = "full" | "half" | "third";

export interface WidgetDef {
  id: string;
  name: string;
  description: string;
  defaultSize: WidgetSize;
  category: string;
}

export interface WidgetConfig {
  id: string;
  visible: boolean;
  size: WidgetSize;
  order: number;
}

export interface PresetLayout {
  name: string;
  widgets: WidgetConfig[];
}

// ─── Dashboard ──────────────────────────────────────────────────────────────

export const DASHBOARD_WIDGETS: WidgetDef[] = [
  { id: "kpi_row_1", name: "Traffic & Search KPIs", description: "New Users, Sessions, Impressions, Clicks", defaultSize: "full", category: "metrics" },
  { id: "kpi_row_2", name: "Performance KPIs", description: "Avg Position, Avg CTR, Crawl Health", defaultSize: "full", category: "metrics" },
  { id: "tech_issues", name: "Technical Issues", description: "Error and warning counts", defaultSize: "third", category: "health" },
  { id: "execution_hub", name: "Execution Hub", description: "Task summary", defaultSize: "third", category: "workflow" },
  { id: "recommendations", name: "Recommendations", description: "Issues to review", defaultSize: "third", category: "workflow" },
  { id: "ai_visibility", name: "AI Visibility", description: "AI search tracking", defaultSize: "half", category: "intelligence" },
  { id: "competitors", name: "Competitors", description: "Competitor summary", defaultSize: "half", category: "intelligence" },
  { id: "search_performance", name: "Search Performance", description: "Top pages and queries", defaultSize: "full", category: "search" },
  { id: "conversion_channels", name: "Conversions by Channel", description: "Attribution breakdown", defaultSize: "half", category: "conversions" },
  { id: "rank_summary", name: "Rank Tracker Summary", description: "Keyword position overview", defaultSize: "half", category: "rankings" },
  { id: "predictions", name: "Traffic Forecast", description: "30/60/90 day predictions", defaultSize: "half", category: "intelligence" },
  { id: "scenarios", name: "What-If Scenarios", description: "Impact modeling", defaultSize: "half", category: "intelligence" },
  { id: "quick_actions", name: "Quick Actions", description: "Shortcut buttons", defaultSize: "full", category: "navigation" },
];

// ─── Report Studio ──────────────────────────────────────────────────────────

export const REPORT_WIDGETS: WidgetDef[] = [
  { id: "executive_summary", name: "Executive Summary", description: "Health score, KPIs, narrative", defaultSize: "full", category: "summary" },
  { id: "wins", name: "What's Working", description: "Top pages, keywords, growth", defaultSize: "full", category: "analysis" },
  { id: "declines", name: "What Needs Attention", description: "Declining pages, tech issues", defaultSize: "full", category: "analysis" },
  { id: "opportunities", name: "Optimization Opportunities", description: "CTR, striking distance", defaultSize: "full", category: "analysis" },
  { id: "ai_insights", name: "AI Insights", description: "Findings, predictions, recommendations", defaultSize: "full", category: "intelligence" },
  { id: "appendix", name: "Data Appendix", description: "Full keyword and page tables", defaultSize: "full", category: "data" },
];

// ─── Conversion Intelligence ────────────────────────────────────────────────

export const CONVERSION_WIDGETS: WidgetDef[] = [
  { id: "conversion_kpis", name: "Conversion KPIs", description: "Conversions, rate, revenue, avg value", defaultSize: "full", category: "metrics" },
  { id: "daily_trend", name: "Daily Trend Chart", description: "Conversion trend over time", defaultSize: "full", category: "charts" },
  { id: "attribution", name: "Channel Attribution", description: "Machine Enhancing attribution breakdown", defaultSize: "full", category: "attribution" },
  { id: "by_type", name: "Conversions by Type", description: "Form submits vs phone calls", defaultSize: "half", category: "breakdown" },
  { id: "top_pages", name: "Top Converting Pages", description: "Pages ranked by conversions", defaultSize: "half", category: "pages" },
  { id: "heatmap_summary", name: "Heatmap Insights", description: "Click + scroll behavior", defaultSize: "full", category: "behavior" },
];

// ─── Lookup helpers ─────────────────────────────────────────────────────────

const ALL_WIDGETS: Record<string, WidgetDef[]> = {
  dashboard: DASHBOARD_WIDGETS,
  report_studio: REPORT_WIDGETS,
  conversion_intelligence: CONVERSION_WIDGETS,
};

export function getWidgetsForPage(page: string): WidgetDef[] {
  return ALL_WIDGETS[page] ?? [];
}

export function getWidgetDef(page: string, widgetId: string): WidgetDef | undefined {
  return getWidgetsForPage(page).find((w) => w.id === widgetId);
}

/** Build the default WidgetConfig[] for a page (all visible, default order + size). */
export function buildDefaultWidgets(page: string): WidgetConfig[] {
  return getWidgetsForPage(page).map((w, i) => ({ id: w.id, visible: true, size: w.defaultSize, order: i }));
}

// ─── Presets ────────────────────────────────────────────────────────────────

function preset(page: string, visibleIds: string[]): WidgetConfig[] {
  const vis = new Set(visibleIds);
  return getWidgetsForPage(page).map((w, i) => ({ id: w.id, visible: vis.has(w.id), size: w.defaultSize, order: i }));
}

export const PRESETS: Record<string, PresetLayout[]> = {
  dashboard: [
    { name: "Full Dashboard", widgets: buildDefaultWidgets("dashboard") },
    { name: "Executive Summary", widgets: preset("dashboard", ["kpi_row_1", "search_performance", "quick_actions"]) },
    { name: "Technical Focus", widgets: preset("dashboard", ["kpi_row_1", "kpi_row_2", "tech_issues", "recommendations"]) },
  ],
  report_studio: [
    { name: "Full Report", widgets: buildDefaultWidgets("report_studio") },
    { name: "Client Summary", widgets: preset("report_studio", ["executive_summary", "wins", "opportunities"]) },
    { name: "Quick Review", widgets: preset("report_studio", ["executive_summary", "wins", "declines"]) },
  ],
  conversion_intelligence: [
    { name: "Full View", widgets: buildDefaultWidgets("conversion_intelligence") },
    { name: "Channel Focus", widgets: preset("conversion_intelligence", ["conversion_kpis", "attribution", "by_type"]) },
    { name: "Page Performance", widgets: preset("conversion_intelligence", ["conversion_kpis", "top_pages", "daily_trend"]) },
  ],
};
