/**
 * Phase Tracking for Vibn Projects
 * 
 * Projects progress through phases with specific agents:
 * 1. Gathering → Collect and analyze all project materials
 * 2. Vision → Extract and validate Product Vision Board
 * 3. Scope → Define V1 MVP features
 * 4. Blueprint → Technical architecture design
 * 5. Build → Implementation (future)
 */

export type ProjectPhase = 
  | 'gathering'    // Phase 1: Collecting context and analyzing materials
  | 'vision'       // Phase 2: Extracting vision board
  | 'scope'        // Phase 3: Defining V1 features
  | 'blueprint'    // Phase 4: Technical design
  | 'build';       // Phase 5: Implementation

export type PhaseStatus = 
  | 'not_started'
  | 'in_progress'
  | 'completed';

export interface PhaseProgress {
  phase: ProjectPhase;
  status: PhaseStatus;
  startedAt?: FirebaseFirestore.Timestamp;
  completedAt?: FirebaseFirestore.Timestamp;
  insights?: GatheringInsight[];  // For gathering phase
  visionBoard?: VisionBoardData;   // For vision phase
  scope?: ScopeData;               // For scope phase
  blueprint?: BlueprintData;       // For blueprint phase
}

export interface GatheringInsight {
  id: string;
  source: string;           // "Patient History Overview"
  sourceType: 'document' | 'github' | 'session' | 'conversation';
  sourceId: string;         // Document/session ID
  insight: string;          // "Using evidence-based diagnostic methods"
  extractedAt: FirebaseFirestore.Timestamp;
  confirmed: boolean;       // User validated this
  confirmedAt?: FirebaseFirestore.Timestamp;
  usedInVision: boolean;    // Phase 2 marked this as used
  category?: 'feature' | 'user' | 'problem' | 'competitor' | 'tech' | 'progress';
}

export interface VisionBoardData {
  vision: string;           // One-sentence vision
  who: string;              // Target users
  need: string;             // Problem they face
  product: string;          // Solution features
  validation: string;       // Go-to-market strategy
  completedAt?: FirebaseFirestore.Timestamp;
  approved: boolean;
  approvedAt?: FirebaseFirestore.Timestamp;
}

export interface ScopeData {
  v1Features: string[];
  timeline: string;
  priorities: {
    mustHave: string[];
    shouldHave: string[];
    niceToHave: string[];
  };
  completedAt?: FirebaseFirestore.Timestamp;
}

export interface BlueprintData {
  techStack: string[];
  architecture: string;
  database: string;
  apis: string[];
  completedAt?: FirebaseFirestore.Timestamp;
}

// Helper to determine which agent to use
export function getAgentForPhase(phase: ProjectPhase): string {
  const agentMap: Record<ProjectPhase, string> = {
    gathering: 'GATHERING_AGENT',
    vision: 'VISION_AGENT',
    scope: 'SCOPE_AGENT',
    blueprint: 'BLUEPRINT_AGENT',
    build: 'BUILD_AGENT'
  };
  return agentMap[phase];
}

// Helper to get next phase
export function getNextPhase(currentPhase: ProjectPhase): ProjectPhase | null {
  const phaseOrder: ProjectPhase[] = ['gathering', 'vision', 'scope', 'blueprint', 'build'];
  const currentIndex = phaseOrder.indexOf(currentPhase);
  return currentIndex < phaseOrder.length - 1 ? phaseOrder[currentIndex + 1] : null;
}

