export type { EmbeddingProvider, VectorSearchResult } from './vector-store.js';
import type { EmbeddingProvider } from './vector-store.js';
import type { StorageAdapter } from '@mosaicstack/storage';

/* ------------------------------------------------------------------ */
/*  Insight types (adapter-level, decoupled from Drizzle schema)      */
/* ------------------------------------------------------------------ */

export interface NewInsight {
  userId: string;
  content: string;
  source: string;
  category: string;
  relevanceScore: number;
  metadata?: Record<string, unknown>;
  embedding?: number[];
}

export interface Insight extends NewInsight {
  id: string;
  createdAt: Date;
  updatedAt?: Date;
  decayedAt?: Date;
}

export interface InsightSearchResult {
  id: string;
  content: string;
  score: number;
  metadata?: Record<string, unknown>;
}

/* ------------------------------------------------------------------ */
/*  MemoryAdapter interface                                           */
/* ------------------------------------------------------------------ */

export interface MemoryAdapter {
  readonly name: string;

  // Preferences
  getPreference(userId: string, key: string): Promise<unknown | null>;
  setPreference(userId: string, key: string, value: unknown, category?: string): Promise<void>;
  deletePreference(userId: string, key: string): Promise<boolean>;
  listPreferences(
    userId: string,
    category?: string,
  ): Promise<Array<{ key: string; value: unknown; category: string }>>;

  // Insights
  storeInsight(insight: NewInsight): Promise<Insight>;
  getInsight(id: string): Promise<Insight | null>;
  /**
   * Searches within one scoped user ID. The reserved `*` query returns scoped
   * recent/all results rather than performing backend-specific wildcard parsing.
   */
  searchInsights(
    userId: string,
    query: string,
    opts?: { limit?: number; embedding?: number[] },
  ): Promise<InsightSearchResult[]>;
  deleteInsight(id: string): Promise<boolean>;

  // Embedding
  readonly embedder: EmbeddingProvider | null;

  // Lifecycle
  close(): Promise<void>;
}

/* ------------------------------------------------------------------ */
/*  MemoryConfig                                                      */
/* ------------------------------------------------------------------ */

export type MemoryConfig =
  | { type: 'pgvector'; embedder?: EmbeddingProvider }
  | { type: 'sqlite-vec'; embedder?: EmbeddingProvider }
  | { type: 'keyword'; storage: StorageAdapter };
