import { calculateCII, type CountryScore } from './country-instability';
import { getCachedCountryScore, normalizeCiiCountryCode } from './cached-risk-scores';
import type { ClusteredEvent } from '@/types';
import type { ThreatLevel } from './threat-classifier';
import { CURATED_COUNTRIES } from '@/config/countries';
import { tokenizeForMatch, matchKeyword } from '@/utils/keyword-match';

export interface StoryData {
  countryCode: string;
  countryName: string;
  cii: {
    score: number;
    level: CountryScore['level'];
    trend: CountryScore['trend'];
    components: CountryScore['components'];
    change24h: number;
  } | null;
  news: Array<{
    title: string;
    threatLevel: ThreatLevel;
    sourceCount: number;
  }>;
  theater: {
    theaterName: string;
    postureLevel: string;
    totalAircraft: number;
    totalVessels: number;
    fighters: number;
    tankers: number;
    awacs: number;
    strikeCapable: boolean;
  } | null;
  markets: Array<{
    title: string;
    yesPrice: number;
  }>;
  threats: {
    critical: number;
    high: number;
    medium: number;
    categories: string[];
  };
  signals: {
    protests: number;
    militaryFlights: number;
    militaryVessels: number;
    outages: number;
    gpsJammingHexes: number;
  };
  convergence: {
    score: number;
    signalTypes: string[];
    regionalDescriptions: string[];
  } | null;
}

export function collectStoryData(
  countryCode: string,
  countryName: string,
  allNews: ClusteredEvent[],
  theaterPostures: Array<{ theaterId: string; theaterName: string; shortName: string; targetNation: string | null; postureLevel: string; totalAircraft: number; totalVessels: number; fighters: number; tankers: number; awacs: number; strikeCapable: boolean }>,
  predictionMarkets: Array<{ title: string; yesPrice: number }>,
  signals?: { protests: number; militaryFlights: number; militaryVessels: number; outages: number; gpsJammingHexes: number },
  convergence?: { score: number; signalTypes: string[]; regionalDescriptions: string[] } | null,
): StoryData {
  const normalizedCountryCode = normalizeCiiCountryCode(countryCode);
  let countryScore: CountryScore | null = getCachedCountryScore(normalizedCountryCode);
  if (!countryScore) {
    const scores = calculateCII();
    countryScore = scores.find(s => s.code === normalizedCountryCode) || null;
  }

  const keywords = CURATED_COUNTRIES[normalizedCountryCode]?.scoringKeywords || [countryName.toLowerCase()];
  const countryNews = allNews.filter(e => {
    const tokens = tokenizeForMatch(e.primaryTitle);
    return keywords.some(kw => matchKeyword(tokens, kw));
  });

  const sortedNews = [...countryNews].sort((a, b) => {
    const priorities: Record<string, number> = { critical: 5, high: 4, medium: 3, low: 2, info: 1 };
    const pa = priorities[a.threat?.level || 'info'] || 0;
    const pb = priorities[b.threat?.level || 'info'] || 0;
    return pb - pa;
  });

  const theater = theaterPostures.find(t =>
    t.targetNation?.toLowerCase() === countryName.toLowerCase() ||
    t.shortName?.toLowerCase() === countryCode.toLowerCase()
  ) || null;

  const countryMarkets = predictionMarkets.filter(m => {
    const mTokens = tokenizeForMatch(m.title);
    return keywords.some(kw => matchKeyword(mTokens, kw));
  });

  const threatCounts = { critical: 0, high: 0, medium: 0, categories: new Set<string>() };
  for (const n of countryNews) {
    const level = n.threat?.level;
    if (level === 'critical') threatCounts.critical++;
    else if (level === 'high') threatCounts.high++;
    else if (level === 'medium') threatCounts.medium++;
    if (n.threat?.category && n.threat.category !== 'general') {
      threatCounts.categories.add(n.threat.category);
    }
  }

  return {
    countryCode: normalizedCountryCode,
    countryName,
    cii: countryScore ? {
      score: countryScore.score,
      level: countryScore.level,
      trend: countryScore.trend,
      components: countryScore.components,
      change24h: countryScore.change24h,
    } : null,
    news: sortedNews.slice(0, 5).map(n => ({
      title: n.primaryTitle,
      threatLevel: (n.threat?.level || 'info') as ThreatLevel,
      sourceCount: n.sourceCount,
    })),
    theater: theater ? {
      theaterName: theater.theaterName,
      postureLevel: theater.postureLevel,
      totalAircraft: theater.totalAircraft,
      totalVessels: theater.totalVessels,
      fighters: theater.fighters,
      tankers: theater.tankers,
      awacs: theater.awacs,
      strikeCapable: theater.strikeCapable,
    } : null,
    markets: countryMarkets.slice(0, 4).map(m => ({
      title: m.title,
      yesPrice: m.yesPrice,
    })),
    threats: {
      critical: threatCounts.critical,
      high: threatCounts.high,
      medium: threatCounts.medium,
      categories: [...threatCounts.categories],
    },
    signals: signals || { protests: 0, militaryFlights: 0, militaryVessels: 0, outages: 0, gpsJammingHexes: 0 },
    convergence: convergence || null,
  };
}
