/**
 * Logging types for monitoring and debugging
 */

import type { ChatMode } from '@/lib/ai/chat-modes';

/**
 * Log entry for project-related events
 * 
 * Stored in Firestore `project_logs` collection for monitoring,
 * debugging, and prompt iteration.
 */
export interface ProjectLogEntry {
  /** Firestore document ID */
  id: string;

  /** Project this log belongs to */
  projectId: string;

  /** User who triggered the event (if available) */
  userId: string | null;

  /** Event type for filtering */
  eventType:
    | 'chat_interaction'
    | 'extraction'
    | 'vision_generation'
    | 'mvp_generation'
    | 'marketing_generation'
    | 'knowledge_import'
    | 'batch_extraction'
    | 'mode_transition'
    | 'error';

  /** Chat mode (if applicable) */
  mode: ChatMode | null;

  /** Project phase (if applicable) */
  phase: string | null;

  /** Which artifacts were used in this interaction */
  artifactsUsed: string[];

  /** Whether vector search was used */
  usedVectorSearch: boolean;

  /** Number of vector chunks retrieved (if applicable) */
  vectorChunkCount?: number;

  /** Prompt version identifier (for A/B testing) */
  promptVersion: string | null;

  /** Model used (e.g., 'gemini-2.0-flash-exp') */
  modelUsed: string | null;

  /** Whether the operation succeeded */
  success: boolean;

  /** Error message (if failed) */
  errorMessage: string | null;

  /** Additional metadata (flexible for future use) */
  metadata?: Record<string, any>;

  /** When this log was created */
  createdAt: Date | string;
}

/**
 * Input for creating a new log entry
 * (id and createdAt are auto-generated)
 */
export type CreateProjectLogInput = Omit<ProjectLogEntry, 'id' | 'createdAt'>;

/**
 * Filters for querying logs
 */
export interface ProjectLogFilters {
  projectId?: string;
  userId?: string;
  eventType?: ProjectLogEntry['eventType'];
  mode?: ChatMode;
  phase?: string;
  success?: boolean;
  startDate?: Date;
  endDate?: Date;
  limit?: number;
}

/**
 * Aggregated log stats for monitoring
 */
export interface ProjectLogStats {
  totalLogs: number;
  successCount: number;
  errorCount: number;
  byEventType: Record<string, number>;
  byMode: Record<string, number>;
  avgVectorChunks: number;
  vectorSearchUsageRate: number;
}

