export interface DataPriority {
  level: 'critical' | 'high' | 'medium' | 'low';
  weight: number;
  expiration: number; // 数据过期时间（秒）
}

export interface ActionDataMapping {
  actionType: string;
  requiredDataSources: string[];
  dataPriority: Record<string, DataPriority>;
  timeRange?: {
    start: number;
    end: number;
  };
}

export interface DataContext {
  actionType: string;
  taskId: string;
  timestamp: number;
  dataRequirements: {
    dataSource: string;
    priority: DataPriority;
    filters?: Record<string, any>;
  }[];
}

export interface DataCacheConfig {
  enabled: boolean;
  ttl: number; // 缓存过期时间（秒）
  sizeLimit: number; // 缓存大小限制（MB）
}

export interface EnhancedDataSource<T, P = unknown> {
  list: (params?: P, context?: DataContext) => Promise<T[]>;
  detail: (id: string, context?: DataContext) => Promise<T>;
  create: (data: Partial<T>, context?: DataContext) => Promise<T>;
  update: (id: string, data: Partial<T>, context?: DataContext) => Promise<T>;
  delete: (id: string, context?: DataContext) => Promise<void>;
  getPriorityData: (params?: P, priority: DataPriority, context?: DataContext) => Promise<T[]>;
  getDataWithContext: (params?: P, context?: DataContext) => Promise<T[]>;
}

export interface DataAbstractionLayer {
  registerDataSource: (name: string, dataSource: EnhancedDataSource<any>) => void;
  getDataSource: (name: string) => EnhancedDataSource<any> | null;
  getActionDataMapping: (actionType: string) => ActionDataMapping | null;
  registerActionDataMapping: (mapping: ActionDataMapping) => void;
  getPrioritizedData: (actionType: string, taskId: string, params?: Record<string, any>) => Promise<Record<string, any>>;
  updateDataContext: (context: DataContext) => Promise<void>;
  getDataContext: (taskId: string) => Promise<DataContext | null>;
  setCacheConfig: (config: DataCacheConfig) => void;
  clearCache: (dataSource?: string) => Promise<void>;
}

export interface ActionDataTable {
  id: string;
  actionType: string;
  taskId: string;
  dataSource: string;
  data: Record<string, any>;
  priority: DataPriority;
  timestamp: number;
  expiresAt: number;
}

export interface DataSyncConfig {
  id: string;
  dataSource: string;
  schedule: string; // cron 表达式
  enabled: boolean;
  lastSync: number;
  nextSync: number;
  syncStatus: 'pending' | 'running' | 'completed' | 'failed';
  error?: string;
}

export interface DataSyncResult {
  success: boolean;
  dataSource: string;
  recordsSynced: number;
  duration: number; // 同步耗时（毫秒）
  error?: string;
  timestamp: number;
}
