/**
 * TypeScript types for the Reputation Blueprint report.
 *
 * These types mirror the backend ReportSynthesis JSON shape
 * stored in pipeline.executions.synthesis (stage5_synthesize_v2.py).
 *
 * ALL labels, vocabulary, and category-specific text come from the
 * synthesis JSON. The frontend has zero category configs.
 */

// =============================================================================
// Score Breakdown
// =============================================================================

export interface ScoreBreakdown {
  rating_quality: number;
  sentiment_depth: number;
  volume: number;
  momentum: number;
  intensity: number;
}

// =============================================================================
// Theme Analysis
// =============================================================================

export interface ThemeScore {
  primitive: string;
  label: string;
  domain: string;
  count: number;
  weight: number;
  valence: {
    positive: number;
    negative: number;
    neutral: number;
    mixed: number;
  };
  intensity: {
    i1: number;
    i2: number;
    i3: number;
  };
  top_quotes: {
    positive: string[];
    negative: string[];
  };
  score_cost?: number;  // reputational cost 0-100
}

// =============================================================================
// Domain Performance
// =============================================================================

export interface DomainScore {
  domain: string;
  label: string;
  score: number;
  weight: number;
  volume: number;
  primitives: string[];
  narrative?: string;
}

// =============================================================================
// Critical Issues
// =============================================================================

export interface CriticalIssue {
  title: string;
  primitive: string;
  domain: string;
  count: number;
  intensity_score: number;
  description: string;
  quotes: string[];
  solution: string;
  complexity: 'quick' | 'medium' | 'complex';
  score_cost?: number;  // reputational cost 0-100
}

// =============================================================================
// Strengths
// =============================================================================

export interface Strength {
  title: string;
  primitive: string;
  domain: string;
  count: number;
  intensity_score: number;
  description: string;
  quotes: string[];
  marketing_angle: string;
}

// =============================================================================
// Action Plan
// =============================================================================

export interface ActionItem {
  action: string;
  source: string;
  owner: string;
  effort: 'low' | 'medium' | 'high';
  timeline: string;
  impact: 'high' | 'medium' | 'low';
  success_metric: string;
  detail?: string;
  evidence?: string;
}

// =============================================================================
// Tracking KPIs
// =============================================================================

export interface KPI {
  metric: string;
  current: string;
  target_30d: string;
  target_90d: string;
}

// =============================================================================
// Evidence
// =============================================================================

export interface Evidence {
  quote: string;
  primitive: string;
  valence: string;
  context: string;
}

// =============================================================================
// Review Evidence (v3 — full review text with classification anchors)
// =============================================================================

export interface ReviewClassification {
  primitive: string;
  valence: string;        // '+', '-', '0', '±'
  anchor_text: string;
  anchor_start: number | null;
  anchor_end: number | null;
}

export interface ReviewEvidence {
  review_id: string;
  author: string;
  rating: number | null;
  date: string | null;
  full_text: string;
  classifications: ReviewClassification[];
}

// =============================================================================
// Chart Data (pre-computed by backend)
// =============================================================================

export interface ChartDataPoint {
  label: string;
  value: number;
  color?: string;
}

export interface DomainRadarPoint {
  axis: string;
  value: number;
}

export interface ThemeMatrixPoint {
  primitive: string;
  label: string;
  positive: number;
  negative: number;
  neutral: number;
  mixed: number;
  total: number;
}

export interface IntensityHeatmapPoint {
  primitive: string;
  label: string;
  i1: number;
  i2: number;
  i3: number;
}

export interface RatingDistPoint {
  rating: number;
  count: number;
}

export interface RatingTrendPoint {
  period: string;
  avg_rating: number;
  review_count: number;
}

export interface MomentumDualPoint {
  period: string;
  positive: number;
  negative: number;
}

export interface QuarterlyRatingPoint {
  quarter: string;       // "2024-Q1"
  avg_rating: number;
  review_count: number;
}

export interface QuarterlyDomainSentimentPoint {
  quarter: string;
  O?: number; P?: number; J?: number; E?: number; V?: number;  // 0-100 positive %
}

export interface SeasonalPatternPoint {
  quarter_label: string; // "Q1"-"Q4"
  avg_rating: number;
  review_count: number;
}

export interface ReportCharts {
  sentiment_donut: ChartDataPoint[];
  domain_radar: DomainRadarPoint[];
  theme_matrix: ThemeMatrixPoint[];
  intensity_heatmap: IntensityHeatmapPoint[];
  rating_distribution: RatingDistPoint[];
  rating_trend: RatingTrendPoint[];
  momentum_dual: MomentumDualPoint[];
  quarterly_rating?: QuarterlyRatingPoint[];
  quarterly_domain_sentiment?: QuarterlyDomainSentimentPoint[];
  seasonal_pattern?: SeasonalPatternPoint[];
}

// =============================================================================
// Staff Leaderboard
// =============================================================================

export interface StaffMember {
  name: string;
  total_mentions: number;
  positive: number;
  negative: number;
  sentiment_score: number;  // 0-100
  positive_quotes?: string[];
  negative_quotes?: string[];
}

export interface StaffIndividual {
  canonical_name: string;
  aliases: string[];
  role_inferred: string | null;
  positive: number;
  negative: number;
  total_mentions: number;
  sentiment_score: number;
  positive_quotes: string[];
  negative_quotes: string[];
  note: string | null;
}

export interface StaffGroup {
  canonical_name: string;
  aliases: string[];
  positive: number;
  negative: number;
  total_mentions: number;
  sentiment_score: number;
  positive_quotes: string[];
  negative_quotes: string[];
  note: string | null;
}

export interface StaffExcluded {
  name: string;
  reason: string;
}

export interface StaffLeaderboardResolved {
  individuals: StaffIndividual[];
  groups: StaffGroup[];
  excluded?: StaffExcluded[];
  observations: string;
}

// =============================================================================
// Top-Level Report Synthesis (matches JSON stored in executions.synthesis)
// =============================================================================

export interface ReportSynthesis {
  // Meta
  report_version: string;
  business_name: string;
  category_label: string;
  sector_code: string;
  report_date: string;
  language?: string;
  review_count: number;

  // Scores
  reputation_score: number; // 0-100
  score_breakdown: ScoreBreakdown;
  current_rating: number;
  potential_rating: number;

  // Rating Distribution
  rating_distribution: Record<string, number>;

  // Executive Summary (LLM-generated with sector vocabulary)
  headline: string;
  verdict: string;
  key_findings: string[];
  revenue_impact: string;

  // AI Narratives (optional — v2.1.0+)
  rating_narrative?: string;
  themes_narrative?: string;
  matrix_narrative?: string;
  trends_narrative?: string;
  domain_overview?: string;
  rating_evolution_narrative?: string;
  domain_sentiment_narrative?: string;
  seasonal_narrative?: string;

  // Themes
  themes: ThemeScore[];

  // Domains
  domains: DomainScore[];

  // Critical Issues (LLM-generated solutions)
  critical_issues: CriticalIssue[];

  // Strengths (LLM-generated marketing angles)
  strengths: Strength[];

  // Action Plan (LLM-generated)
  actions: ActionItem[];

  // Tracking
  kpis: KPI[];

  // Evidence
  evidence: Evidence[];

  // Staff Leaderboard (array = legacy v2.0.0, object = resolved v2.1.0+)
  staff_leaderboard?: StaffMember[] | StaffLeaderboardResolved;

  // Review Evidence (v3 — full review text with classification anchors)
  review_evidence?: ReviewEvidence[];

  // Methodology (v2.2.0+)
  methodology?: {
    data_source: string;
    oldest_review: string | null;
    newest_review: string | null;
    review_count: number;
    classification_model: string;
    score_weights: Record<string, number>;
  };

  // Conclusion (v2.2.0+)
  conclusion?: {
    takeaways: string[];
    ninety_day_focus: string;
    review_cadence: string;
    cost_of_inaction: string;
  };

  // Pre-computed chart data
  charts: ReportCharts;
}
