import api from './index';

// Dashboard
// export const getDashboardStats = () => api.get<any, any>('/dashboard/stats'); // Deprecated

export const getTodayEnergy = () => api.get<any, { value: number }>('/dashboard/stats/energy');
export const getRealtimePower = () => api.get<any, { value: number }>('/dashboard/stats/power');
export const getDeviceOverview = () => api.get<any, { active_alarms: number, online_rate: number, total_devices: number }>('/dashboard/stats/overview');
export const getEnergyTrend = () => api.get<any, { trend: number[], dates?: string[] }>('/dashboard/stats/trend');
export const getPowerTrend = () => api.get<any, { trend: number[], times?: string[] }>('/dashboard/stats/power-trend');
export const getRecentActivities = () => api.get<any, { activities: any[] }>('/dashboard/stats/activities');

// Control
export const controlDevice = (id: string, action: 'on' | 'off') => {
  return api.post(`/control/${id}?action=${action}`);
};

// Alarms
export interface Alarm {
  ID: string;
  DeviceID: string;
  Type: string;
  Content: string;
  Status: string;
  StartTime: string;
}

export const getAlarms = (status?: string) => {
  return api.get<any, Alarm[]>('/alarms', { params: { status } });
};

export const acknowledgeAlarm = (id: string) => {
  return api.put(`/alarms/${id}/ack`);
};

// Alarm Rules
export interface AlarmRule {
  id: string;
  name: string;
  target_type: 'DEVICE' | 'SPACE';
  target_id: string;
  metric: string;
  operator: string;
  threshold: number;
  duration: number;
  silence_period: number;
  priority: string;
  message: string;
  enabled: boolean;
  created_at?: string;
}

export const getAlarmRules = (params?: { target_id?: string, name?: string }) => {
  return api.get<any, AlarmRule[] | { data: AlarmRule[], total: number }>('/alarm-rules', { params });
};

export const createAlarmRule = (data: Partial<AlarmRule>) => {
  return api.post<any, AlarmRule>('/alarm-rules', data);
};

export const updateAlarmRule = (id: string, data: Partial<AlarmRule>) => {
  return api.put<any, AlarmRule>(`/alarm-rules/${id}`, data);
};

export const deleteAlarmRule = (id: string) => {
  return api.delete(`/alarm-rules/${id}`);
};

export const batchDeleteAlarmRules = (ids: string[]) => {
  return api.delete('/alarm-rules/batch', { data: { ids } });
};

// Analysis
export const getEnergyAnalysis = (period: 'day' | 'month') => {
  return api.get<any, any[]>('/analysis/energy', { params: { period } });
};

