export type Provider = 'samsung_health';

// These match exactly what the Android companion app emits from Health Connect
// (see android/.../health/HealthReader.kt) and what the backend stores in
// VALID_METRICS (backend/app/models/device_reading.py).
//
// Samsung Health limitation: a few intuitive categories (hydration, active
// calories, HRV, body temperature, respiratory rate, resting HR) are NOT
// written from Samsung Health to Health Connect on Android, so we don't
// support them. To get those, we'd need to integrate Samsung Health Data SDK
// directly instead of going through Health Connect.
export type MetricId =
  | 'steps'
  | 'calories_total'
  | 'calories_intake'
  | 'sleep_duration'
  | 'heart_rate_resting'
  | 'blood_oxygen'
  | 'weight'
  | 'body_fat'
  | 'basal_metabolic_rate'
  | 'blood_glucose'
  | 'blood_pressure_sys'
  | 'blood_pressure_dia';

// Metrics that represent a single accumulated value per day (steps walked,
// calories eaten, calories burned, sleep hours). These render as bar charts.
// Continuous-state metrics (HR, weight, SpO2, BP, glucose) render as line/area.
export const DAILY_TOTAL_METRICS: MetricId[] = [
  'steps',
  'calories_total',
  'calories_intake',
  'sleep_duration',
];

export interface MetricConfig {
  id: MetricId;
  label: string;
  unit: string;
  icon: string;
  goodDirection: 'up' | 'down' | 'neutral';
  normalRange?: { min: number; max: number };
  format: (value: number) => string;
}

export const METRIC_CONFIG: Record<MetricId, MetricConfig> = {
  steps: {
    id: 'steps', label: 'Steps', unit: 'steps', icon: 'Footprints',
    goodDirection: 'up', normalRange: { min: 7000, max: 15000 },
    format: (v) => v.toLocaleString(),
  },
  calories_total: {
    id: 'calories_total', label: 'Calories Burned', unit: 'kcal', icon: 'Flame',
    goodDirection: 'neutral',
    format: (v) => Math.round(v).toLocaleString(),
  },
  calories_intake: {
    id: 'calories_intake', label: 'Food Intake', unit: 'kcal', icon: 'Apple',
    goodDirection: 'neutral',
    format: (v) => Math.round(v).toLocaleString(),
  },
  sleep_duration: {
    id: 'sleep_duration', label: 'Sleep', unit: 'hrs', icon: 'Moon',
    goodDirection: 'neutral', normalRange: { min: 7, max: 9 },
    format: (v) => `${Math.floor(v)}h ${Math.round((v % 1) * 60)}m`,
  },
  heart_rate_resting: {
    id: 'heart_rate_resting', label: 'Heart Rate', unit: 'bpm', icon: 'Heart',
    goodDirection: 'down', normalRange: { min: 50, max: 90 },
    format: (v) => `${Math.round(v)}`,
  },
  blood_oxygen: {
    id: 'blood_oxygen', label: 'SpO2', unit: '%', icon: 'Wind',
    goodDirection: 'up', normalRange: { min: 95, max: 100 },
    format: (v) => `${v.toFixed(1)}%`,
  },
  weight: {
    id: 'weight', label: 'Weight', unit: 'kg', icon: 'Scale',
    goodDirection: 'neutral',
    format: (v) => `${v.toFixed(1)}`,
  },
  body_fat: {
    id: 'body_fat', label: 'Body Fat', unit: '%', icon: 'Activity',
    goodDirection: 'down', normalRange: { min: 10, max: 25 },
    format: (v) => `${v.toFixed(1)}%`,
  },
  basal_metabolic_rate: {
    id: 'basal_metabolic_rate', label: 'BMR', unit: 'kcal', icon: 'Battery',
    goodDirection: 'neutral',
    format: (v) => Math.round(v).toLocaleString(),
  },
  blood_glucose: {
    id: 'blood_glucose', label: 'Blood Glucose', unit: 'mg/dL', icon: 'Droplets',
    goodDirection: 'neutral', normalRange: { min: 70, max: 140 },
    format: (v) => `${Math.round(v)}`,
  },
  blood_pressure_sys: {
    id: 'blood_pressure_sys', label: 'BP Systolic', unit: 'mmHg', icon: 'HeartPulse',
    goodDirection: 'down', normalRange: { min: 90, max: 130 },
    format: (v) => `${Math.round(v)}`,
  },
  blood_pressure_dia: {
    id: 'blood_pressure_dia', label: 'BP Diastolic', unit: 'mmHg', icon: 'HeartPulse',
    goodDirection: 'down', normalRange: { min: 60, max: 85 },
    format: (v) => `${Math.round(v)}`,
  },
};

export const PROVIDER_CONFIG: Record<Provider, { name: string; brandColor: string; description: string }> = {
  samsung_health: {
    name: 'Samsung Health',
    brandColor: '#1428A0',
    description: 'Galaxy Watch and Samsung Health app data via the Hipograf companion app',
  },
};

export interface APIDeviceConnection {
  _id: string;
  userId: string;
  provider: Provider;
  status: 'active' | 'expired' | 'revoked' | 'pending';
  lastSyncAt?: string;
  sharedWithPractitioners: boolean;
  metadata: Record<string, unknown>;
  created_at?: string;
  updated_at?: string;
}

export interface APIDeviceReading {
  _id: string;
  userId: string;
  provider: Provider;
  metric: string;
  date: string;
  value: number;
  unit: string;
  metricType: string;
  subMetrics: Record<string, unknown>;
}

export interface MetricSummary {
  value: number;
  unit: string;
  date: string;
  provider: Provider;
  subMetrics?: Record<string, unknown>;
}

export interface DeviceSummaryResponse {
  summary: Record<string, MetricSummary>;
  connectedProviders: Provider[];
  lastSync: string | null;
}
