export const STANDARD_WIDGETS = [
  {
    id: 'pending-review',
    title: '待处理审核',
    description: '展示待当前用户处理的数据审核任务。',
    icon: 'mdi-clipboard-check-outline',
    color: 'indigo'
  },
  {
    id: 'order-status',
    title: '数据订单状态',
    description: '汇总数据订单的待审核、处理中和已完成状态。',
    icon: 'mdi-receipt-text-check-outline',
    color: 'blue'
  },
  {
    id: 'product-statistics',
    title: '数据产品统计',
    description: '展示已发布数据产品及其近期变化。',
    icon: 'mdi-package-variant-closed',
    color: 'teal'
  },
  {
    id: 'datasource-health',
    title: '数据源健康度',
    description: '汇总数据源连接状态与异常情况。',
    icon: 'mdi-database-check-outline',
    color: 'green'
  },
  {
    id: 'n8n-execution-status',
    title: 'n8n 执行状态',
    description: '展示 n8n 工作流最近执行结果和失败提醒。',
    icon: 'mdi-source-branch-check',
    color: 'deep-orange'
  }
]

function storageKey (username) {
  return `dataops-workbench-layout:${username}`
}

export function defaultWorkbenchLayout () {
  return STANDARD_WIDGETS.map(widget => ({ id: widget.id, enabled: true }))
}

export function loadWorkbenchLayout (username) {
  try {
    const saved = JSON.parse(localStorage.getItem(storageKey(username)))
    if (!Array.isArray(saved)) return defaultWorkbenchLayout()
    const known = saved.filter(item => STANDARD_WIDGETS.some(widget => widget.id === item.id))
    const missing = STANDARD_WIDGETS
      .filter(widget => !known.some(item => item.id === widget.id))
      .map(widget => ({ id: widget.id, enabled: true }))
    return [...known, ...missing]
  } catch (error) {
    return defaultWorkbenchLayout()
  }
}

export function saveWorkbenchLayout (username, layout) {
  localStorage.setItem(storageKey(username), JSON.stringify(layout))
}
