/**
 * Analytics Types
 * TypeScript definitions for analytics, scoring, and insights
 */

// Score interfaces
export interface ActivityScore {
  score: number; // 0 to 1
  factors: string[];
  breakdown: {
    completeness: number;
    consistency: number;
    duration: number;
    categorization: number;
  };
}

export interface ContentScore {
  score: number; // 0 to 1
  factors: string[];
  breakdown: {
    notesQuality: number;
    titleClarity: number;
    internalNotes: number;
    technicalDetail: number;
  };
}

export interface TimelinessScore {
  score: number; // 0 to 1
  factors: string[];
  breakdown: {
    entryDelay: number;
    businessHours: number;
    regularity: number;
    approvalTimeliness: number;
  };
}

// Insight interfaces
export interface AnalyticsInsight {
  type: 'success' | 'warning' | 'error' | 'info';
  category: 'activity' | 'content' | 'timeliness' | 'overall' | 'billing' | 'performance';
  title: string;
  description: string;
  recommendation: string;
  severity?: 'low' | 'medium' | 'high';
  actionable?: boolean;
}

// Analysis interfaces
export interface TimeEntryAnalysis {
  timeEntryId: number;
  activityScore: ActivityScore;
  contentScore: ContentScore;
  timelinessScore: TimelinessScore;
  overallScore: number; // 0 to 1
  insights: AnalyticsInsight[];
  analyzedAt: Date;
}

export interface AggregateAnalysis {
  totalEntries: number;
  totalHours: number;
  averageHoursPerEntry: number;
  dateRange: {
    earliest: Date;
    latest: Date;
  };
  scores: {
    activity: number;
    content: number;
    timeliness: number;
    overall: number;
  };
  insights: AnalyticsInsight[];
  patterns: {
    dayOfWeek: number[]; // 7 values, Sunday = 0
    hourly: number[]; // 24 values
  };
  trends: {
    weekly: Array<{
      week: Date;
      hours: number;
      entries: number;
    }>;
  };
  analyzedAt: Date;
}

// Timeline interfaces
export interface TimelineEvent {
  id: string;
  type: 'time_entry' | 'key_moment' | 'milestone';
  timestamp: Date;
  title: string;
  description?: string;
  duration?: number; // in hours
  metadata?: Record<string, any>;
  score?: number; // 0 to 1
  isHumanActivity: boolean;
  importance: 'low' | 'medium' | 'high' | 'critical';
}

export interface TimelineView {
  events: TimelineEvent[];
  dateRange: {
    start: Date;
    end: Date;
  };
  timeRange: 'hour' | 'day' | 'week' | 'month';
  filters: {
    resourceIds?: number[];
    ticketIds?: number[];
    projectIds?: number[];
    activityTypes?: string[];
    minScore?: number;
  };
  summary: {
    totalEvents: number;
    humanActivities: number;
    systemActivities: number;
    totalHours: number;
    averageScore: number;
  };
}

// LLM Analysis interfaces
export interface LLMAnalysisRequest {
  timeEntries: Array<{
    id: number;
    notes?: string;
    title?: string;
    hours_worked: number;
    entry_date: string;
    resource_name?: string;
    ticket_title?: string;
  }>;
  analysisType: 'productivity' | 'quality' | 'patterns' | 'anomalies' | 'comprehensive';
  context?: {
    timeRange: string;
    resourceIds?: number[];
    projectIds?: number[];
  };
}

export interface LLMAnalysisResponse {
  insights: string[];
  patterns: Array<{
    type: string;
    description: string;
    frequency: number;
    impact: 'low' | 'medium' | 'high';
  }>;
  recommendations: Array<{
    category: string;
    priority: 'low' | 'medium' | 'high';
    action: string;
    expectedImpact: string;
  }>;
  summary: {
    overallQuality: number; // 0 to 1
    productivityLevel: number; // 0 to 1
    keyFindings: string[];
  };
  processingTime: number; // milliseconds
  tokensUsed: number;
}

// Scoring algorithm interfaces
export interface ScoringWeights {
  activity: number;
  content: number;
  timeliness: number;
}

export interface ScoringConfiguration {
  weights: ScoringWeights;
  thresholds: {
    excellent: number;
    good: number;
    average: number;
    poor: number;
  };
  factors: {
    activity: {
      completeness: number;
      consistency: number;
      duration: number;
      categorization: number;
    };
    content: {
      notesQuality: number;
      titleClarity: number;
      internalNotes: number;
      technicalDetail: number;
    };
    timeliness: {
      entryDelay: number;
      businessHours: number;
      regularity: number;
      approvalTimeliness: number;
    };
  };
}

// Analytics query interfaces
export interface AnalyticsQuery {
  timeRange: {
    start: Date;
    end: Date;
  };
  filters: {
    resourceIds?: number[];
    ticketIds?: number[];
    taskIds?: number[];
    projectIds?: number[];
    companyIds?: number[];
    minHours?: number;
    maxHours?: number;
    billable?: boolean;
    approved?: boolean;
    activityTypes?: string[];
  };
  groupBy?: 'resource' | 'ticket' | 'project' | 'company' | 'day' | 'week' | 'month';
  includeScores?: boolean;
  includeInsights?: boolean;
  includePatterns?: boolean;
  includeTrends?: boolean;
}

export interface AnalyticsQueryResult {
  data: Array<{
    group: string;
    totalEntries: number;
    totalHours: number;
    averageHoursPerEntry: number;
    scores?: {
      activity: number;
      content: number;
      timeliness: number;
      overall: number;
    };
    insights?: AnalyticsInsight[];
  }>;
  summary: AggregateAnalysis;
  query: AnalyticsQuery;
  processedAt: Date;
}

// Export and reporting interfaces
export interface AnalyticsExport {
  format: 'csv' | 'excel' | 'pdf' | 'json';
  data: {
    timeEntries: any[];
    analyses: TimeEntryAnalysis[];
    summary: AggregateAnalysis;
    insights: AnalyticsInsight[];
  };
  metadata: {
    exportedAt: Date;
    timeRange: string;
    filters: string;
    recordCount: number;
  };
}

// Performance metrics
export interface AnalyticsPerformanceMetrics {
  processingTime: number; // milliseconds
  recordsProcessed: number;
  recordsPerSecond: number;
  memoryUsage: number; // MB
  cacheHitRate: number; // percentage
  errors: string[];
}
