// 持续记忆系统
import { generateId } from '../../utils/id';
import { createLogger } from '../../utils/logger';

const log = createLogger('Memory');

export interface UserProfile {
  id: string;
  preferences: {
    language: 'zh' | 'en';
    timezone: string;
    responseStyle: 'concise' | 'detailed';
  };
  patterns: {
    activeHours: number[];
    frequentCommands: string[];
  };
}

export interface MemoryEvent {
  id: string;
  userId: string;
  type: string;
  content: any;
  timestamp: Date;
  embedding?: number[];
}

export class PersistentMemorySystem {
  private profiles: Map<string, UserProfile> = new Map();
  private events: MemoryEvent[] = [];

  async remember(userId: string, event: MemoryEvent): Promise<void> {
    event.id = event.id || this.generateId();
    event.timestamp = new Date();
    this.events.push(event);
    log.debug(`Event remembered: ${event.id} for user ${userId}`);
  }

  async recall(userId: string, query: string, limit: number = 10): Promise<MemoryEvent[]> {
    // TODO: 实现向量搜索（后续实现）
    return this.events.filter(e => e.userId === userId).slice(0, limit);
  }

  async getProfile(userId: string): Promise<UserProfile | undefined> {
    return this.profiles.get(userId);
  }

  async updateProfile(userId: string, updates: Partial<UserProfile>): Promise<void> {
    let profile = this.profiles.get(userId);
    if (!profile) {
      profile = {
        id: userId,
        preferences: { language: 'zh', timezone: 'Asia/Shanghai', responseStyle: 'concise' },
        patterns: { activeHours: [], frequentCommands: [] },
      };
      this.profiles.set(userId, profile);
    }
    Object.assign(profile, updates);
    log.debug(`Profile updated for user ${userId}`);
  }

  async getEventCount(userId: string): Promise<number> {
    return this.events.filter(e => e.userId === userId).length;
  }

  private generateId(): string {
    return generateId('mem');
  }
}
