import { ProjectStatus } from "./enums.js";

export const DASHBOARD_LAYOUT_VERSION = 2;
export const DASHBOARD_GRID_COLUMNS = 12;

export interface DashboardWidgetSize {
  w: number;
  h: number;
}

export interface DashboardWidgetBaseConfig {
  showDetails?: boolean;
}

export interface StatCardsWidgetConfig extends DashboardWidgetBaseConfig {}

export interface ResourceTableWidgetConfig extends DashboardWidgetBaseConfig {
  chapter?: string;
}

export interface ProjectTableWidgetConfig extends DashboardWidgetBaseConfig {
  search?: string;
  status?: ProjectStatus;
}

export interface PeakTimesWidgetConfig extends DashboardWidgetBaseConfig {
  granularity?: "week" | "month";
  groupBy?: "project" | "chapter" | "resource";
}

export interface DemandWidgetConfig extends DashboardWidgetBaseConfig {
  groupBy?: "project" | "person" | "chapter";
}

export interface TopValueResourcesWidgetConfig extends DashboardWidgetBaseConfig {
  limit?: number;
}

export interface ChargeabilityOverviewWidgetConfig extends DashboardWidgetBaseConfig {
  topN?: number;
  watchlistThreshold?: number;
}

export interface MyProjectsWidgetConfig extends DashboardWidgetBaseConfig {
  showFavorites?: boolean;
  showResponsible?: boolean;
}

export interface BudgetForecastWidgetConfig extends DashboardWidgetBaseConfig {}

export interface SkillGapWidgetConfig extends DashboardWidgetBaseConfig {}

export interface ProjectHealthWidgetConfig extends DashboardWidgetBaseConfig {}

export interface DashboardWidgetConfigMap {
  "stat-cards": StatCardsWidgetConfig;
  "resource-table": ResourceTableWidgetConfig;
  "project-table": ProjectTableWidgetConfig;
  "peak-times-chart": PeakTimesWidgetConfig;
  "demand-view": DemandWidgetConfig;
  "top-value-resources": TopValueResourcesWidgetConfig;
  "chargeability-overview": ChargeabilityOverviewWidgetConfig;
  "my-projects": MyProjectsWidgetConfig;
  "budget-forecast": BudgetForecastWidgetConfig;
  "skill-gap": SkillGapWidgetConfig;
  "project-health": ProjectHealthWidgetConfig;
}

export const DASHBOARD_WIDGET_TYPES = [
  "stat-cards",
  "resource-table",
  "project-table",
  "peak-times-chart",
  "demand-view",
  "top-value-resources",
  "chargeability-overview",
  "my-projects",
  "budget-forecast",
  "skill-gap",
  "project-health",
] as const;

export type DashboardWidgetType = (typeof DASHBOARD_WIDGET_TYPES)[number];
export type DashboardWidgetConfig = DashboardWidgetConfigMap[DashboardWidgetType];

export interface DashboardWidgetCatalogEntry<T extends DashboardWidgetType = DashboardWidgetType> {
  type: T;
  label: string;
  description: string;
  icon: string;
  defaultSize: DashboardWidgetSize;
  minSize: DashboardWidgetSize;
  defaultConfig: DashboardWidgetConfigMap[T];
}

export interface DashboardWidgetInstance<T extends DashboardWidgetType = DashboardWidgetType> {
  id: string;
  type: T;
  title?: string;
  x: number;
  y: number;
  w: number;
  h: number;
  minW: number;
  minH: number;
  config: DashboardWidgetConfigMap[T];
}

export interface DashboardLayoutConfig {
  version: number;
  widgets: DashboardWidgetInstance[];
  gridCols: number;
}

export const DASHBOARD_WIDGET_CATALOG = [
  {
    type: "stat-cards",
    label: "Overview Stats",
    description: "Key metrics: total resources, active projects, allocations, budget utilization",
    icon: "📊",
    defaultSize: { w: 12, h: 3 },
    minSize: { w: 6, h: 2 },
    defaultConfig: { showDetails: false },
  },
  {
    type: "resource-table",
    label: "Resource Table",
    description: "Filterable list of EIDs with utilization and chargeability",
    icon: "👥",
    defaultSize: { w: 8, h: 6 },
    minSize: { w: 4, h: 4 },
    defaultConfig: { showDetails: false },
  },
  {
    type: "project-table",
    label: "Project Overview",
    description: "Projects with costs, person days, and timeline",
    icon: "📋",
    defaultSize: { w: 8, h: 6 },
    minSize: { w: 4, h: 4 },
    defaultConfig: { showDetails: false },
  },
  {
    type: "peak-times-chart",
    label: "Peak Times",
    description: "Booked hours vs capacity over time",
    icon: "📈",
    defaultSize: { w: 8, h: 5 },
    minSize: { w: 4, h: 4 },
    defaultConfig: {
      showDetails: false,
      granularity: "month",
      groupBy: "project",
    },
  },
  {
    type: "demand-view",
    label: "Demand View",
    description: "Staffing demand vs supply by project, person, or chapter",
    icon: "🔍",
    defaultSize: { w: 6, h: 5 },
    minSize: { w: 4, h: 4 },
    defaultConfig: {
      showDetails: false,
      groupBy: "project",
    },
  },
  {
    type: "top-value-resources",
    label: "Top Value Resources",
    description: "Leaderboard of resources ranked by price/quality value score",
    icon: "★",
    defaultSize: { w: 6, h: 5 },
    minSize: { w: 4, h: 4 },
    defaultConfig: {
      showDetails: false,
      limit: 10,
    },
  },
  {
    type: "chargeability-overview",
    label: "Chargeability Overview",
    description: "Top-list and watchlist by actual chargeability this month",
    icon: "⚡",
    defaultSize: { w: 6, h: 8 },
    minSize: { w: 4, h: 6 },
    defaultConfig: {
      showDetails: false,
      topN: 10,
      watchlistThreshold: 15,
    },
  },
  {
    type: "my-projects",
    label: "My Projects",
    description: "Quick access to your favorite and responsible projects",
    icon: "⭐",
    defaultSize: { w: 6, h: 6 },
    minSize: { w: 4, h: 3 },
    defaultConfig: {
      showDetails: false,
      showFavorites: true,
      showResponsible: true,
    },
  },
  {
    type: "budget-forecast",
    label: "Budget Forecast",
    description: "Budget burn rate and projected exhaustion per active project",
    icon: "💰",
    defaultSize: { w: 6, h: 5 },
    minSize: { w: 4, h: 4 },
    defaultConfig: { showDetails: false },
  },
  {
    type: "skill-gap",
    label: "Skill Gap Analysis",
    description: "Top skill shortages: open demand vs available supply",
    icon: "🎯",
    defaultSize: { w: 6, h: 5 },
    minSize: { w: 4, h: 4 },
    defaultConfig: { showDetails: false },
  },
  {
    type: "project-health",
    label: "Project Health",
    description: "Composite health score per project: budget, staffing, timeline",
    icon: "🏥",
    defaultSize: { w: 6, h: 5 },
    minSize: { w: 4, h: 4 },
    defaultConfig: { showDetails: false },
  },
] as const satisfies readonly DashboardWidgetCatalogEntry[];
