/**
 * Training service TypeScript type definitions
 * Mirrored from backend API schemas
 */

// Enums
export enum TrainingStatus {
  PENDING = 'pending',
  RUNNING = 'running',
  COMPLETED = 'completed',
  FAILED = 'failed',
  CANCELLED = 'cancelled',
}

// Request types
export interface TrainingJobRequest {
  products?: string[]; // optional array of product IDs
  start_date?: string; // ISO 8601 date string, optional
  end_date?: string; // ISO 8601 date string, optional
}

export interface SingleProductTrainingRequest {
  start_date?: string; // ISO 8601 date string
  end_date?: string; // ISO 8601 date string
  seasonality_mode?: string; // 'additive' | 'multiplicative'
  daily_seasonality?: boolean;
  weekly_seasonality?: boolean;
  yearly_seasonality?: boolean;
  bakery_location?: [number, number]; // [latitude, longitude]
}

// Response types
export interface TrainingResults {
  total_products: number;
  successful_trainings: number;
  failed_trainings: number;
  products: any[]; // Product-specific results
  overall_training_time_seconds: number;
}

export interface DataSummary {
  // Will be populated based on actual backend response structure
  [key: string]: any;
}

export interface ProcessingMetadata {
  background_task: boolean;
  async_execution: boolean;
  enhanced_features: boolean;
  repository_pattern: boolean;
}

export interface TrainingJobResponse {
  job_id: string;
  tenant_id: string;
  status: TrainingStatus;
  message: string;
  created_at: string; // ISO 8601 date string
  estimated_duration_minutes: number;
  training_results: TrainingResults;
  data_summary?: DataSummary | null;
  completed_at?: string | null; // ISO 8601 date string
  error_details?: string | null;
  processing_metadata: ProcessingMetadata;
}

export interface TrainingJobStatus {
  job_id: string;
  status: TrainingStatus;
  progress?: number; // 0-100 percentage
  message?: string;
  current_step?: string;
  estimated_time_remaining?: number; // seconds
}

export interface TrainingJobProgress {
  progress: {
    percentage: number;
    current_step: string;
    estimated_time_remaining: number;
    products_completed: number;
    products_total: number;
  };
}

// Model types
export interface TrainingMetrics {
  mape: number; // Mean Absolute Percentage Error
  mae: number; // Mean Absolute Error
  rmse: number; // Root Mean Square Error
  r2_score: number; // R-squared score
}

export interface TrainingPeriod {
  start_date: string; // ISO 8601 date string
  end_date: string; // ISO 8601 date string
}

export interface ActiveModelResponse {
  model_id: string;
  model_path: string;
  features_used: string[];
  hyperparameters: Record<string, any>;
  training_metrics: TrainingMetrics;
  created_at: string; // ISO 8601 date string
  training_period: TrainingPeriod;
}

export interface ModelMetricsResponse {
  model_id: string;
  metrics: TrainingMetrics;
  created_at: string;
  training_period: TrainingPeriod;
}

export interface TrainedModelResponse {
  model_id: string;
  tenant_id: string;
  inventory_product_id: string;
  status: string;
  model_type: string;
  training_metrics: TrainingMetrics;
  created_at: string;
  training_period: TrainingPeriod;
  features_used: string[];
  hyperparameters: Record<string, any>;
}

// Statistics types
export interface TenantStatistics {
  total_models: number;
  active_models: number;
  training_jobs_count: number;
  average_accuracy: number;
  last_training_date?: string;
}

export interface ModelPerformanceResponse {
  model_id: string;
  performance_metrics: TrainingMetrics;
  validation_results: Record<string, any>;
  feature_importance: Record<string, number>;
}

// WebSocket message types
export interface TrainingProgressMessage {
  type: 'progress';
  job_id: string;
  progress: TrainingJobProgress['progress'];
}

export interface TrainingCompletedMessage {
  type: 'completed';
  job_id: string;
  results: {
    training_results: TrainingResults;
    performance_metrics: TrainingMetrics;
    successful_trainings: number;
    training_duration: number; // in seconds
  };
}

export interface TrainingErrorMessage {
  type: 'error';
  job_id: string;
  error: string;
}

export interface TrainingStartedMessage {
  type: 'started';
  job_id: string;
  message: string;
}

export interface TrainingCancelledMessage {
  type: 'cancelled';
  job_id: string;
  message: string;
}

export type TrainingWebSocketMessage = 
  | TrainingProgressMessage 
  | TrainingCompletedMessage 
  | TrainingErrorMessage 
  | TrainingStartedMessage
  | TrainingCancelledMessage;

// Query parameter types
export interface ModelsQueryParams {
  status?: string;
  model_type?: string;
  limit?: number;
  offset?: number;
}

// API response wrappers
export interface PaginatedResponse<T> {
  data: T[];
  total: number;
  limit: number;
  offset: number;
  has_next: boolean;
  has_previous: boolean;
}

// Export all types
export type {
  // Add any additional export aliases if needed
};