export type KevoFlagCategory =
  | "KNOWLEDGE_GAP"
  | "WRONG_INFO"
  | "DATA_UPDATE_REQUEST"
  | "ESCALATION"
  | "OTHER";
export type KevoFlagStatus =
  | "NEW"
  | "IN_REVIEW"
  | "APPROVED"
  | "REJECTED"
  | "PUBLISHED"
  | "ROUTED"
  | "RESOLVED";
export type KevoFlagSource = "IN_CALL" | "POST_CALL" | "MANUAL";

export interface KevoFlag {
  id: string;
  reference: string;
  source: KevoFlagSource;
  category: KevoFlagCategory;
  status: KevoFlagStatus;
  conversation_id: string;
  member_id: string | null;
  member_question: string | null;
  agent_answer: string | null;
  member_claim: string | null;
  entity: string | null;
  requested_change: string | null;
  reviewer_id: string | null;
  reviewer_notes: string | null;
  clickup_task_id: string | null;
  clickup_task_url: string | null;
  created_at: string;
  updated_at: string | null;
}

export interface KevoFlagFilters {
  categories?: KevoFlagCategory[];
  statuses?: KevoFlagStatus[];
  sources?: KevoFlagSource[];
  entity?: string;
  search?: string;
  date_from?: string;
  date_to?: string;
  page: number;
  limit: number;
}

export interface KevoAuditRow {
  id: string;
  flag_id: string | null;
  entry_id: string | null;
  action: string;
  actor_email: string | null;
  metadata: Record<string, unknown> | null;
  created_at: string;
}

export interface KevoFlagDetail {
  flag: KevoFlag;
  audit: KevoAuditRow[];
  dashboard_url: string;
}

export interface KevoStats {
  by_status: Array<{ status: KevoFlagStatus; count: number }>;
  weekly: Array<{ week: string; category: KevoFlagCategory; count: number }>;
  top_entities: Array<{ entity: string; count: number }>;
  avg_resolution_hours: number | null;
}

export type KevoEntryCategory = "amenity" | "policy" | "faq" | "correction";
export type KevoEntryStatus = "DRAFT" | "APPROVED" | "PUBLISHED" | "RETIRED";

export interface KevoKnowledgeEntry {
  id: string;
  flag_id: string | null;
  entity: string;
  aliases: string[];
  property: string | null;
  category: KevoEntryCategory;
  verified_text: string;
  status: KevoEntryStatus;
  approved_by: string | null;
  approved_at: string | null;
  published_at: string | null;
  version: number;
  created_at: string;
  updated_at: string | null;
}

export interface KevoTranscriptTurn {
  role: string;
  message: string | null;
  time_in_call_secs: number | null;
  tool_calls: Array<{ tool_name: string | null; params: string | null }>;
  tool_results: Array<{ tool_name: string | null; result: string | null }>;
}

export interface KevoConversation {
  conversation_id: string;
  started_at: string | null;
  duration_secs: number | null;
  summary: string | null;
  call_successful: string | null;
  dashboard_url: string;
  turns: KevoTranscriptTurn[];
}

export interface KevoPublishPreview {
  content: string;
  hash: string;
  entries_count: number;
  document_id: string | null;
  last_published_hash: string | null;
  last_published_at: string | null;
  unchanged: boolean;
}

export interface KevoPublishLogRow {
  id: string;
  kb_document_id: string | null;
  entries_count: number;
  content_hash: string;
  published_by: string;
  published_at: string;
  result: string;
}
