/**
 * Knowledge and Memory related type definitions
 */

import type { UUID, Timestamps } from './common.types.js';
import type { ImmigrationCategory } from './immigration.types.js';

// Document/Knowledge types
export interface Document extends Timestamps {
  id: UUID;
  title: string;
  content: string;
  category?: ImmigrationCategory;
  tags: string[];
  source?: string;
  sourceUrl?: string;
  metadata?: DocumentMetadata;
  isActive: boolean;
}

export interface DocumentMetadata {
  author?: string;
  publishDate?: Date;
  lastVerifiedDate?: Date;
  version?: string;
  language?: string;
}

export interface DocumentChunk {
  id: UUID;
  documentId: UUID;
  chunkIndex: number;
  content: string;
  embedding?: number[];
  metadata?: Record<string, unknown>;
}

// RAG Search types
export interface SearchQuery {
  query: string;
  category?: ImmigrationCategory;
  limit?: number;
  threshold?: number;
  useGraph?: boolean;
}

export interface SearchResult {
  documentId: UUID;
  chunkId: UUID;
  content: string;
  score: number;
  metadata?: Record<string, unknown>;
  document?: Document;
}

// Memory types (Long-term memory in Knowledge Graph)
export enum MemoryType {
  FACT = 'FACT',           // User-stated facts
  PREFERENCE = 'PREFERENCE', // User preferences
  INTERACTION = 'INTERACTION', // Interaction summary
  ASSESSMENT = 'ASSESSMENT',  // Assessment results
  INTENT = 'INTENT',       // User intentions
}

export interface Memory extends Timestamps {
  id: UUID;
  userId: UUID;
  type: MemoryType;
  content: string;
  category?: ImmigrationCategory;
  confidence: number;
  source: MemorySource;
  relatedMemoryIds?: UUID[];
  expiresAt?: Date;
}

export enum MemorySource {
  USER_STATED = 'USER_STATED',
  INFERRED = 'INFERRED',
  ASSESSMENT = 'ASSESSMENT',
}

// Knowledge Graph types
export interface KnowledgeNode {
  id: UUID;
  type: KnowledgeNodeType;
  label: string;
  properties: Record<string, unknown>;
}

export enum KnowledgeNodeType {
  TOPIC = 'TOPIC',
  CONCEPT = 'CONCEPT',
  REQUIREMENT = 'REQUIREMENT',
  PROCEDURE = 'PROCEDURE',
  DOCUMENT = 'DOCUMENT',
  FAQ = 'FAQ',
}

export interface KnowledgeRelation {
  id: UUID;
  sourceId: UUID;
  targetId: UUID;
  type: RelationType;
  weight?: number;
  properties?: Record<string, unknown>;
}

export enum RelationType {
  RELATED_TO = 'RELATED_TO',
  REQUIRES = 'REQUIRES',
  PART_OF = 'PART_OF',
  FOLLOWS = 'FOLLOWS',
  CONTRADICTS = 'CONTRADICTS',
  SUPERSEDES = 'SUPERSEDES',
}

// Evolution/Experience types
export interface Experience extends Timestamps {
  id: UUID;
  type: ExperienceType;
  pattern: string;
  insight: string;
  frequency: number;
  confidence: number;
  category?: ImmigrationCategory;
  metadata?: ExperienceMetadata;
}

export enum ExperienceType {
  FAQ_PATTERN = 'FAQ_PATTERN',
  USER_CONCERN = 'USER_CONCERN',
  EFFECTIVE_RESPONSE = 'EFFECTIVE_RESPONSE',
  CONVERSION_PATTERN = 'CONVERSION_PATTERN',
  CONFUSION_POINT = 'CONFUSION_POINT',
}

export interface ExperienceMetadata {
  sourceConversationIds?: UUID[];
  firstSeenAt?: Date;
  lastSeenAt?: Date;
  sampleQuestions?: string[];
  sampleResponses?: string[];
}

// Evolution types
export interface Evolution extends Timestamps {
  id: UUID;
  triggeredBy: UUID;
  type: EvolutionType;
  changes: EvolutionChanges;
  status: EvolutionStatus;
  appliedAt?: Date;
  rollbackAt?: Date;
}

export enum EvolutionType {
  PROMPT_UPDATE = 'PROMPT_UPDATE',
  KNOWLEDGE_UPDATE = 'KNOWLEDGE_UPDATE',
  RULE_UPDATE = 'RULE_UPDATE',
  BEHAVIOR_UPDATE = 'BEHAVIOR_UPDATE',
}

export enum EvolutionStatus {
  PROPOSED = 'PROPOSED',
  APPROVED = 'APPROVED',
  APPLIED = 'APPLIED',
  ROLLED_BACK = 'ROLLED_BACK',
  REJECTED = 'REJECTED',
}

export interface EvolutionChanges {
  promptChanges?: PromptChange[];
  knowledgeChanges?: KnowledgeChange[];
  ruleChanges?: RuleChange[];
}

export interface PromptChange {
  section: string;
  oldValue?: string;
  newValue: string;
  reason: string;
}

export interface KnowledgeChange {
  action: 'ADD' | 'UPDATE' | 'DELETE';
  documentId?: UUID;
  content?: string;
  reason: string;
}

export interface RuleChange {
  ruleName: string;
  oldValue?: unknown;
  newValue: unknown;
  reason: string;
}

// Admin document management
export interface CreateDocumentDto {
  title: string;
  content: string;
  category?: ImmigrationCategory;
  tags?: string[];
  source?: string;
  sourceUrl?: string;
}

export interface UpdateDocumentDto {
  title?: string;
  content?: string;
  category?: ImmigrationCategory;
  tags?: string[];
  isActive?: boolean;
}
