export type MediaType = "text-only" | "single-image" | "carousel" | "video" | "mixed";

export interface CommentInsights {
  themes: string[];
  sentiment: string;
  topReactions: string[];
  audienceQuestions: string[];
  whyPeopleEngage: string;
}

export interface VisualAnalysis {
  hasImages: boolean;
  layout: string;
  colorMood: string;
  textOnImage: boolean;
  typographyStyle: string;
  visualHook: string;
  replicationTips: string[];
  imageGenPrompt: string;
}

export interface ViralAnalysis {
  whyViral: string[];
  hookPattern: string;
  structurePattern: string;
  emotionalTrigger: string;
  timingAngle: string;
  commentInsights: CommentInsights;
  visualAnalysis: VisualAnalysis;
  replicationStrategy: string;
  keyTakeaways: string[];
  analyzedAt: string;
}

export function parseViralAnalysis(raw: string | null | undefined): ViralAnalysis | null {
  if (!raw) return null;
  try {
    return JSON.parse(raw) as ViralAnalysis;
  } catch {
    return null;
  }
}

export function parseMediaUrls(raw: string | null | undefined): string[] {
  if (!raw) return [];
  try {
    const parsed = JSON.parse(raw) as unknown;
    return Array.isArray(parsed) ? parsed.filter((u): u is string => typeof u === "string") : [];
  } catch {
    return [];
  }
}