export interface ChatMessage {
  id: string;
  role: "user" | "assistant";
  content: string;
  timestamp: number;
}

export interface StreamChunk {
  token: string;
}

export interface ChatRequest {
  messages: { role: "user" | "assistant"; content: string }[];
  conversationId?: string;
  personId?: string;
}

// --- Observer Output (M3) ---

export interface EngagementSignals {
  replied: boolean;
  latency_seconds: number;
  substantive: boolean;
  word_count: number;
  sentiment_score: number;
  sentiment_direction: "positive" | "negative" | "neutral";
}

export interface StyleProfile {
  formality: number;
  uses_lowercase: boolean;
  uses_jargon: boolean;
  structure: "stream_of_thought" | "structured" | "narrative" | "technical";
  emoji: boolean;
}

export interface TopicAnalysis {
  primary: string;
  domain: string;
  specificity: "surface" | "intermediate" | "expert";
  continued_from_previous: boolean;
  deepened: boolean;
}

export interface ConversationDynamics {
  redirected: boolean;
  redirect_direction: string;
  who_is_leading: "person" | "system";
  built_on_previous: boolean;
  corrected_system: boolean;
}

export interface ObserverOutput {
  engagement: EngagementSignals;
  style: StyleProfile;
  topic: TopicAnalysis;
  dynamics: ConversationDynamics;
}

export type SignalDimension = "engagement" | "style" | "topic" | "dynamics";

export interface SignalMemory {
  dimension: SignalDimension;
  content: string;
  confidence: number;
  tags: string[];
}

// --- Person & Cohorts (M4) ---

export interface CohortMembership {
  cohort: string;
  probability: number;
}

export interface PersonProfile {
  personId: string;
  interactionCount: number;
  avgFormality: number;
  avgSentiment: number;
  avgWordCount: number;
  jargonRate: number;
  leaderRate: number;
  topSpecificity: "surface" | "intermediate" | "expert";
  topDomains: string[];
  cohorts: CohortMembership[];
  updatedAt: number;
}

export interface CohortDefinition {
  name: string;
  description: string;
  match: (profile: PersonProfile) => number; // returns 0-1 probability
}

// --- Communication Brief (M5) ---

export interface TopicEntry {
  topic: string;
  domain: string;
  frequency: number;
  specificity: "surface" | "intermediate" | "expert";
  deepened: boolean;
}

export interface CommunicationBrief {
  personId: string;
  interactionCount: number;
  assembledAt: number;

  topics: {
    hot: TopicEntry[];
    cold: TopicEntry[];
    domains: string[];
  };

  style: {
    formality: "casual" | "moderate" | "formal";
    length: "terse" | "moderate" | "verbose";
    structure: string;
    usesJargon: boolean;
    usesEmoji: boolean;
  };

  patterns: {
    leadsConversation: boolean;
    deepensTopics: boolean;
    avgSentiment: number;
    sentimentTrend: "warming" | "stable" | "cooling";
  };

  observations: string[];

  cohortPriors: {
    active: boolean;
    weight: number;
    priors: string[];
  };

  assemblyMs: number;
}

// --- Conversations ---

export interface Conversation {
  id: string;
  title: string;
  createdAt: number;
  lastMessageAt: number;
}

// --- Store ---

export interface ChatState {
  messages: ChatMessage[];
  isStreaming: boolean;
  error: string | null;

  conversations: Conversation[];
  activeConversationId: string | null;
  personId: string;

  addUserMessage: (content: string) => string;
  startStreaming: () => string;
  appendToken: (id: string, token: string) => void;
  finishStreaming: (id: string) => void;
  setError: (error: string | null) => void;
  clearMessages: () => void;

  createConversation: () => string;
  switchConversation: (id: string) => void;
  setMessages: (msgs: ChatMessage[]) => void;
  updateConversationTitle: (id: string, title: string) => void;
  deleteConversation: (id: string) => void;
  switchPerson: () => string;
}
