import api from './client'

export type WidgetType =
  | 'ProductionStats'
  | 'QueueStatus'
  | 'RecentRenders'
  | 'CostOverview'
  | 'WorkerStatus'
  | 'KPISummary'
  | 'OrderThroughput'
  | 'RevenueChart'
  | 'ItemStatus'
  | 'ProcessingTimes'
  | 'RenderTimeByOutputType'
  | 'OutputTypeUsage'
  | 'TopProducts'
  | 'OrdersByUser'
  | 'RenderBackendStats'

export interface WidgetPosition {
  col: number
  row: number
  w: number
  h: number
}

export interface WidgetConfig {
  widget_type: WidgetType
  position: WidgetPosition
  config?: Record<string, unknown>
}

interface DashboardConfigResponse {
  widgets: WidgetConfig[]
}

export async function getDashboardConfig(): Promise<WidgetConfig[]> {
  const { data } = await api.get<DashboardConfigResponse>('/dashboard/config')
  return data.widgets
}

export async function updateDashboardConfig(
  widgets: WidgetConfig[]
): Promise<WidgetConfig[]> {
  const { data } = await api.put<DashboardConfigResponse>('/dashboard/config', {
    widgets,
  })
  return data.widgets
}

export async function getTenantDefaultDashboard(): Promise<WidgetConfig[]> {
  const { data } = await api.get<DashboardConfigResponse>(
    '/dashboard/tenant-default'
  )
  return data.widgets
}

export async function updateTenantDefaultDashboard(
  widgets: WidgetConfig[]
): Promise<WidgetConfig[]> {
  const { data } = await api.put<DashboardConfigResponse>(
    '/dashboard/tenant-default',
    { widgets }
  )
  return data.widgets
}
