/**
 * Widget Registry - Central registry for all available widget types
 *
 * Each widget type has a unique key and metadata for rendering and configuration
 */

import {
  Clock,
  User,
  Zap,
  BarChart3,
  Calendar,
  Activity,
  Bell,
  CheckSquare,
  ShoppingCart,
  MessageSquare,
  Headphones,
  Cloud,
  FileText,
  ClipboardCheck,
} from 'lucide-react';
import type { ComponentType } from 'react';

/** Widget size presets */
export type WidgetSize = 'small' | 'medium' | 'large' | 'wide' | 'tall';

/** Widget category for grouping in the add widget dialog */
export type WidgetCategory = 'general' | 'productivity' | 'analytics' | 'communication' | 'integrations';

/** Widget metadata definition */
export interface WidgetMeta {
  /** Unique identifier for the widget type */
  type: string;
  /** Display name (translation key) */
  nameKey: string;
  /** Description (translation key) */
  descriptionKey: string;
  /** Icon component */
  icon: ComponentType<{ className?: string }>;
  /** Category for grouping */
  category: WidgetCategory;
  /** Default size */
  defaultSize: WidgetSize;
  /** Minimum grid columns the widget needs */
  minCols: number;
  /** Minimum grid rows the widget needs */
  minRows: number;
  /** Whether the widget can be resized */
  resizable: boolean;
  /** Required roles to use this widget (empty = all roles) */
  requiredRoles: string[];
}

/** Predefined widget sizes in grid units */
export const WIDGET_SIZE_MAP: Record<WidgetSize, { cols: number; rows: number }> = {
  small: { cols: 1, rows: 1 },
  medium: { cols: 1, rows: 2 },
  large: { cols: 2, rows: 2 },
  wide: { cols: 2, rows: 1 },
  tall: { cols: 1, rows: 3 },
};

/** Registry of all available widgets */
export const WIDGET_REGISTRY: Record<string, WidgetMeta> = {
  clock: {
    type: 'clock',
    nameKey: 'widgets.clock.name',
    descriptionKey: 'widgets.clock.description',
    icon: Clock,
    category: 'general',
    defaultSize: 'small',
    minCols: 1,
    minRows: 1,
    resizable: true,
    requiredRoles: [],
  },
  welcome: {
    type: 'welcome',
    nameKey: 'widgets.welcome.name',
    descriptionKey: 'widgets.welcome.description',
    icon: User,
    category: 'general',
    defaultSize: 'wide',
    minCols: 2,
    minRows: 1,
    resizable: false,
    requiredRoles: [],
  },
  quickActions: {
    type: 'quickActions',
    nameKey: 'widgets.quickActions.name',
    descriptionKey: 'widgets.quickActions.description',
    icon: Zap,
    category: 'productivity',
    defaultSize: 'medium',
    minCols: 1,
    minRows: 2,
    resizable: true,
    requiredRoles: [],
  },
  stats: {
    type: 'stats',
    nameKey: 'widgets.stats.name',
    descriptionKey: 'widgets.stats.description',
    icon: BarChart3,
    category: 'analytics',
    defaultSize: 'small',
    minCols: 1,
    minRows: 1,
    resizable: true,
    requiredRoles: [],
  },
  calendar: {
    type: 'calendar',
    nameKey: 'widgets.calendar.name',
    descriptionKey: 'widgets.calendar.description',
    icon: Calendar,
    category: 'productivity',
    defaultSize: 'medium',
    minCols: 1,
    minRows: 2,
    resizable: true,
    requiredRoles: [],
  },
  activity: {
    type: 'activity',
    nameKey: 'widgets.activity.name',
    descriptionKey: 'widgets.activity.description',
    icon: Activity,
    category: 'general',
    defaultSize: 'large',
    minCols: 2,
    minRows: 2,
    resizable: true,
    requiredRoles: [],
  },
  notifications: {
    type: 'notifications',
    nameKey: 'widgets.notifications.name',
    descriptionKey: 'widgets.notifications.description',
    icon: Bell,
    category: 'communication',
    defaultSize: 'medium',
    minCols: 1,
    minRows: 2,
    resizable: true,
    requiredRoles: [],
  },
  tasks: {
    type: 'tasks',
    nameKey: 'widgets.tasks.name',
    descriptionKey: 'widgets.tasks.description',
    icon: CheckSquare,
    category: 'productivity',
    defaultSize: 'medium',
    minCols: 1,
    minRows: 2,
    resizable: true,
    requiredRoles: [],
  },
  // Integration Widgets
  orders: {
    type: 'orders',
    nameKey: 'widgets.orders.name',
    descriptionKey: 'widgets.orders.description',
    icon: ShoppingCart,
    category: 'integrations',
    defaultSize: 'medium',
    minCols: 1,
    minRows: 2,
    resizable: true,
    requiredRoles: ['manager', 'admin'],
  },
  chat: {
    type: 'chat',
    nameKey: 'widgets.chat.name',
    descriptionKey: 'widgets.chat.description',
    icon: MessageSquare,
    category: 'integrations',
    defaultSize: 'medium',
    minCols: 1,
    minRows: 2,
    resizable: true,
    requiredRoles: [],
  },
  todoistTasks: {
    type: 'todoistTasks',
    nameKey: 'widgets.todoistTasks.name',
    descriptionKey: 'widgets.todoistTasks.description',
    icon: CheckSquare,
    category: 'integrations',
    defaultSize: 'medium',
    minCols: 1,
    minRows: 2,
    resizable: true,
    requiredRoles: [],
  },
  tickets: {
    type: 'tickets',
    nameKey: 'widgets.tickets.name',
    descriptionKey: 'widgets.tickets.description',
    icon: Headphones,
    category: 'integrations',
    defaultSize: 'medium',
    minCols: 1,
    minRows: 2,
    resizable: true,
    requiredRoles: ['manager', 'admin'],
  },
  files: {
    type: 'files',
    nameKey: 'widgets.files.name',
    descriptionKey: 'widgets.files.description',
    icon: Cloud,
    category: 'integrations',
    defaultSize: 'medium',
    minCols: 1,
    minRows: 2,
    resizable: true,
    requiredRoles: [],
  },
  documents: {
    type: 'documents',
    nameKey: 'widgets.documents.name',
    descriptionKey: 'widgets.documents.description',
    icon: FileText,
    category: 'integrations',
    defaultSize: 'large',
    minCols: 2,
    minRows: 2,
    resizable: true,
    requiredRoles: [],
  },
  gembadocs: {
    type: 'gembadocs',
    nameKey: 'widgets.gembadocs.name',
    descriptionKey: 'widgets.gembadocs.description',
    icon: ClipboardCheck,
    category: 'integrations',
    defaultSize: 'large',
    minCols: 2,
    minRows: 2,
    resizable: true,
    requiredRoles: ['manager', 'admin'],
  },
} as const;

/** Get all widgets by category */
export function getWidgetsByCategory(category: WidgetCategory): WidgetMeta[] {
  return Object.values(WIDGET_REGISTRY).filter((widget) => widget.category === category);
}

/** Get a widget by type */
export function getWidgetMeta(type: string): WidgetMeta | undefined {
  return WIDGET_REGISTRY[type];
}

/** Check if user has access to a widget based on roles */
export function hasWidgetAccess(widgetType: string, userRoles: string[]): boolean {
  const widget = WIDGET_REGISTRY[widgetType];
  if (!widget) return false;
  if (widget.requiredRoles.length === 0) return true;
  return widget.requiredRoles.some((role) => userRoles.includes(role));
}

/** Get available widgets for a user based on their roles */
export function getAvailableWidgets(userRoles: string[]): WidgetMeta[] {
  return Object.values(WIDGET_REGISTRY).filter((widget) => hasWidgetAccess(widget.type, userRoles));
}

/** Widget categories with translations */
export const WIDGET_CATEGORIES: { key: WidgetCategory; labelKey: string }[] = [
  { key: 'general', labelKey: 'widgets.categories.general' },
  { key: 'productivity', labelKey: 'widgets.categories.productivity' },
  { key: 'analytics', labelKey: 'widgets.categories.analytics' },
  { key: 'communication', labelKey: 'widgets.categories.communication' },
  { key: 'integrations', labelKey: 'widgets.categories.integrations' },
];
