/**
 * Farm Dashboard - Config constants and type definitions
 * Row IDs, Card IDs, and mapping for disable/reorder functionality
 */

export const ROW_IDS = [
  'overviewKpis',
  'weatherAlerts',
  'sensorMonitoring',
  'sensorCharts',
  'alertsWater',
  'predictions',
  'soilHeatmap',
  'ndviRecommendations',
  'economic'
] as const

export type RowId = (typeof ROW_IDS)[number]

export const CARD_IDS = [
  'farmOverviewKpis',
  'farmWeatherCard',
  'farmAlertsTracker',
  'sensorValuesList',
  'sensorRadarChart',
  'sensorComparisonChart',
  'anomalyDetectionCard',
  'farmAlertsTimeline',
  'waterNeedPrediction',
  'harvestPredictionCard',
  'yieldPredictionChart',
  'soilMoistureHeatmap',
  'ndviHealthCard',
  'recommendationsList',
  'economicOverview'
] as const

export type CardId = (typeof CARD_IDS)[number]

/** Maps each card to its parent row */
export const CARD_TO_ROW: Record<CardId, RowId> = {
  farmOverviewKpis: 'overviewKpis',
  farmWeatherCard: 'weatherAlerts',
  farmAlertsTracker: 'weatherAlerts',
  sensorValuesList: 'sensorMonitoring',
  sensorRadarChart: 'sensorMonitoring',
  sensorComparisonChart: 'sensorCharts',
  anomalyDetectionCard: 'sensorCharts',
  farmAlertsTimeline: 'alertsWater',
  waterNeedPrediction: 'alertsWater',
  harvestPredictionCard: 'predictions',
  yieldPredictionChart: 'predictions',
  soilMoistureHeatmap: 'soilHeatmap',
  ndviHealthCard: 'ndviRecommendations',
  recommendationsList: 'ndviRecommendations',
  economicOverview: 'economic'
}

/** Grid size for each card - matches layout in page.tsx */
export const CARD_GRID_SIZE: Record<CardId, { xs?: number; sm?: number; md?: number; lg: number }> = {
  farmOverviewKpis: { xs: 12, sm: 6, md: 4, lg: 12 }, // Full row of 6 KPIs - parent handles
  farmWeatherCard: { xs: 12, md: 6, lg: 4 },
  farmAlertsTracker: { xs: 12, md: 6, lg: 8 },
  sensorValuesList: { xs: 12, lg: 5 },
  sensorRadarChart: { xs: 12, lg: 7 },
  sensorComparisonChart: { xs: 12, lg: 8 },
  anomalyDetectionCard: { xs: 12, lg: 4 },
  farmAlertsTimeline: { xs: 12, lg: 4 },
  waterNeedPrediction: { xs: 12, lg: 8 },
  harvestPredictionCard: { xs: 12, md: 6, lg: 4 },
  yieldPredictionChart: { xs: 12, lg: 12 },
  soilMoistureHeatmap: { xs: 12, lg: 12 },
  ndviHealthCard: { xs: 12, md: 6, lg: 4 },
  recommendationsList: { xs: 12, md: 6, lg: 8 },
  economicOverview: { xs: 12, lg: 12 }
}

/** Display label for each card (for settings UI) */
export const CARD_LABELS: Record<CardId, string> = {
  farmOverviewKpis: 'Overview KPIs',
  farmWeatherCard: 'Weather',
  farmAlertsTracker: 'Alerts Tracker',
  sensorValuesList: 'Sensor Values',
  sensorRadarChart: 'Sensor Radar Chart',
  sensorComparisonChart: 'Sensor Comparison',
  anomalyDetectionCard: 'Anomaly Detection',
  farmAlertsTimeline: 'Alerts Timeline',
  waterNeedPrediction: 'Water Need Prediction',
  harvestPredictionCard: 'Harvest Prediction',
  yieldPredictionChart: 'Yield Prediction',
  soilMoistureHeatmap: 'Soil Moisture Heatmap',
  ndviHealthCard: 'NDVI Health',
  recommendationsList: 'Recommendations',
  economicOverview: 'Economic Overview'
}

/** Display label for each row (for drag handle / settings) */
export const ROW_LABELS: Record<RowId, string> = {
  overviewKpis: 'Overview KPIs',
  weatherAlerts: 'Weather & Alerts',
  sensorMonitoring: 'Sensor Monitoring',
  sensorCharts: 'Sensor Charts',
  alertsWater: 'Alerts & Water Prediction',
  predictions: 'Predictions',
  soilHeatmap: 'Soil Moisture Heatmap',
  ndviRecommendations: 'NDVI & Recommendations',
  economic: 'Economic Overview'
}

/** Cards that belong to each row (for rendering) */
export const ROW_CARDS: Record<RowId, CardId[]> = {
  overviewKpis: ['farmOverviewKpis'],
  weatherAlerts: ['farmWeatherCard', 'farmAlertsTracker'],
  sensorMonitoring: ['sensorValuesList', 'sensorRadarChart'],
  sensorCharts: ['sensorComparisonChart', 'anomalyDetectionCard'],
  alertsWater: ['farmAlertsTimeline', 'waterNeedPrediction'],
  predictions: ['harvestPredictionCard', 'yieldPredictionChart'],
  soilHeatmap: ['soilMoistureHeatmap'],
  ndviRecommendations: ['ndviHealthCard', 'recommendationsList'],
  economic: ['economicOverview']
}

export interface FarmDashboardConfig {
  disabledCardIds: string[]
  rowOrder: string[]
  enableDragReorder?: boolean
}

/** Default config when no backend data */
export const DEFAULT_FARM_DASHBOARD_CONFIG: FarmDashboardConfig = {
  disabledCardIds: [],
  rowOrder: [...ROW_IDS],
  enableDragReorder: true
}
