import api from './index';

// --- Types ---

export interface IntegrationSource {
  ID: string;
  Name: string;
  DriverType: 'HOME_ASSISTANT' | 'MODBUS_TCP' | 'MQTT' | 'HTTP';
  Config: any; // stringified JSON or object
  Status: string;
  DeviceCount?: number;
}

export interface HAEntity {
  entity_id: string;
  state: string;
  attributes: any;
  last_changed: string;
  last_updated: string;
  device_id?: string;
  device_name?: string;
}

export interface Device {
  ID: string;
  Name: string;
  ExternalID?: string;
  OriginalName?: string;
  DeviceType: string;
  Status: string;
  RatedPower: number;
  LocationID?: string | null;
  SourceID?: string;
  MeteringMode?: string;
  AttributeMapping?: Record<string, string>;
}

export interface Location {
  ID: string;
  ParentID?: string;
  Name: string;
  Type: string;
  children?: Location[];
}

// --- Source APIs ---

export const getSources = () => {
  return api.get<any, IntegrationSource[]>('/sources');
};

export const createSource = (data: Partial<IntegrationSource>) => {
  return api.post<any, IntegrationSource>('/sources', data);
};

export const updateSource = (id: string, data: Partial<IntegrationSource>) => {
  return api.put<any, IntegrationSource>(`/sources/${id}`, data);
};

export const deleteSource = (id: string) => {
  return api.delete(`/sources/${id}`);
};

export const testSourceConnection = (data: Partial<IntegrationSource>) => {
  return api.post('/sources/test', data);
};

export const syncSource = (id: string) => {
  return api.post(`/sources/${id}/sync`);
};

export const getSourceCandidates = (id: string) => {
  return api.get<any, HAEntity[]>(`/sources/${id}/candidates`);
};

export const getSourceDevices = (id: string) => {
  return api.get<any, {id: string, name: string}[]>(`/sources/${id}/devices`);
};

export const getSourceDeviceEntities = (id: string, deviceId: string) => {
  return api.get<any, HAEntity[]>(`/sources/${id}/devices/${deviceId}/entities`);
};

export const callSourceService = (id: string, data: { domain: string; service: string; service_data: any }) => {
  return api.post<any, any>(`/sources/${id}/service`, data);
};

// --- Device APIs ---

export interface DeviceHistoryParams {
  device_ids: string;
  metric?: string;
  start?: string;
  end?: string;
  interval?: string;
}

export interface HistoryData {
  ts: string;
  open: number;
  high: number;
  low: number;
  close: number;
  avg: number;
  device_id: string;
}

export const getDevices = (params?: any) => {
  return api.get<any, Device[]>('/devices', { params });
};

export const getDevice = (id: string) => {
  return api.get<any, Device>(`/devices/${id}`);
};

export const getDeviceRealtime = (id: string) => {
  return api.get<any, { metric: string, value: number, ts: string }[]>(`/devices/${id}/realtime`);
};

export const getDeviceHistory = (params: DeviceHistoryParams) => {
  return api.get<any, HistoryData[]>('/devices/history', { params });
};

export const deleteDeviceHistory = (params: { device_id: string; metric: string; start: string; end: string }) => {
  return api.delete('/devices/history', { params });
};

export const createDevice = (data: Partial<Device>) => {
  return api.post<any, Device>('/devices', data);
};

export const updateDevice = (id: string, data: Partial<Device>) => {
  return api.put<any, Device>(`/devices/${id}`, data);
};

export const deleteDevice = (id: string) => {
  return api.delete(`/devices/${id}`);
};

// --- Location APIs ---

export const getLocations = () => {
  return api.get<any, Location[]>('/locations');
};

export const createLocation = (data: Partial<Location>) => {
  return api.post<any, Location>('/locations', data);
};

export const updateLocation = (id: string, data: Partial<Location>) => {
  return api.put<any, Location>(`/locations/${id}`, data);
};

export const deleteLocation = (id: string) => {
  return api.delete(`/locations/${id}`);
};
