/**
 * Agent Types for LocalGreenChain
 * Defines common interfaces and types for all autonomous agents
 */

export type AgentStatus = 'idle' | 'running' | 'paused' | 'error' | 'completed';
export type AgentPriority = 'low' | 'medium' | 'high' | 'critical';

export interface AgentConfig {
  id: string;
  name: string;
  description: string;
  enabled: boolean;
  intervalMs: number;
  priority: AgentPriority;
  maxRetries: number;
  timeoutMs: number;
}

export interface AgentTask {
  id: string;
  agentId: string;
  type: string;
  payload: Record<string, any>;
  priority: AgentPriority;
  createdAt: string;
  scheduledFor?: string;
  status: 'pending' | 'running' | 'completed' | 'failed';
  result?: any;
  error?: string;
  retryCount: number;
}

export interface AgentMetrics {
  agentId: string;
  tasksCompleted: number;
  tasksFailed: number;
  averageExecutionMs: number;
  lastRunAt: string | null;
  lastSuccessAt: string | null;
  lastErrorAt: string | null;
  uptime: number;
  errors: AgentError[];
}

export interface AgentError {
  timestamp: string;
  message: string;
  taskId?: string;
  stack?: string;
}

export interface AgentEvent {
  id: string;
  agentId: string;
  eventType: 'task_started' | 'task_completed' | 'task_failed' | 'agent_started' | 'agent_stopped' | 'alert';
  timestamp: string;
  data: Record<string, any>;
}

export interface AgentAlert {
  id: string;
  agentId: string;
  severity: 'info' | 'warning' | 'error' | 'critical';
  title: string;
  message: string;
  timestamp: string;
  acknowledged: boolean;
  actionRequired?: string;
  relatedEntityId?: string;
  relatedEntityType?: string;
}

export interface BaseAgent {
  config: AgentConfig;
  status: AgentStatus;
  metrics: AgentMetrics;

  start(): Promise<void>;
  stop(): Promise<void>;
  pause(): void;
  resume(): void;
  runOnce(): Promise<AgentTask | null>;
  getMetrics(): AgentMetrics;
  getAlerts(): AgentAlert[];
}

// Recommendation types
export interface PlantingRecommendation {
  id: string;
  growerId: string;
  produceType: string;
  recommendedQuantity: number;
  quantityUnit: string;
  expectedYieldKg: number;
  projectedRevenue: number;
  riskLevel: 'low' | 'medium' | 'high';
  explanation: string;
  demandSignalIds: string[];
  timestamp: string;
}

export interface EnvironmentRecommendation {
  plantId: string;
  category: string;
  currentValue: any;
  recommendedValue: any;
  priority: 'low' | 'medium' | 'high';
  reason: string;
  expectedImpact: string;
}

export interface MarketMatch {
  id: string;
  supplyId: string;
  demandSignalId: string;
  produceType: string;
  matchedQuantityKg: number;
  pricePerKg: number;
  deliveryDistanceKm: number;
  carbonFootprintKg: number;
  matchScore: number;
  timestamp: string;
}

export interface SustainabilityReport {
  periodStart: string;
  periodEnd: string;
  totalCarbonSavedKg: number;
  totalFoodMilesSaved: number;
  localProductionPercentage: number;
  wasteReductionPercentage: number;
  waterSavedLiters: number;
  recommendations: string[];
}

export interface NetworkAnalysis {
  totalNodes: number;
  totalConnections: number;
  clusters: {
    centroid: { lat: number; lon: number };
    nodeCount: number;
    avgDistance: number;
    dominantSpecies: string[];
  }[];
  hotspots: {
    location: { lat: number; lon: number };
    intensity: number;
    type: 'grower' | 'consumer' | 'mixed';
  }[];
  recommendations: string[];
}

export interface QualityReport {
  chainId: string;
  isValid: boolean;
  blocksVerified: number;
  integrityScore: number;
  issues: {
    blockIndex: number;
    issueType: string;
    description: string;
    severity: 'low' | 'medium' | 'high' | 'critical';
  }[];
  lastVerifiedAt: string;
}
