/**
 * FF AI Agent Memories API Client.
 * Backend: /api/v1/agent/memories  (CRUD; ?scope=... &category=...)
 * 三个正交维度：ownerScope（USER/ORG）/ scope（GLOBAL/PROJECT/PERSONA）/ category（USER/FEEDBACK/PROJECT/REFERENCE）。
 */

import { createCrudClient } from './_crud-factory';

export type MemoryScope = 'GLOBAL' | 'PROJECT' | 'PERSONA';
export type MemoryOwnerScope = 'USER' | 'ORG';
export type MemoryCategory = 'USER' | 'FEEDBACK' | 'PROJECT' | 'REFERENCE';

export const MEMORY_CATEGORIES: readonly MemoryCategory[] = [
  'USER',
  'FEEDBACK',
  'PROJECT',
  'REFERENCE',
];

export interface AgentMemory {
  id: string;
  organizationId: string;
  createdById: string | null;
  ownerScope: MemoryOwnerScope;
  content: string;
  scope: MemoryScope;
  category: MemoryCategory;
  projectId: string | null;
  personaId: string | null;
  source: string | null;
  createdAt: string;
  updatedAt: string;
}

export interface CreateAgentMemoryInput {
  content: string;
  scope?: MemoryScope;
  category?: MemoryCategory;
  projectId?: string;
  personaId?: string;
  source?: string;
}

export interface UpdateAgentMemoryInput {
  content?: string;
  category?: MemoryCategory;
}

const client = createCrudClient<AgentMemory, CreateAgentMemoryInput, UpdateAgentMemoryInput>(
  '/agent/memories',
);

export const listAgentMemories = (filter?: { scope?: MemoryScope; category?: MemoryCategory }) => {
  const params: string[] = [];
  if (filter?.scope) params.push(`scope=${filter.scope}`);
  if (filter?.category) params.push(`category=${filter.category}`);
  return client.list(params.length ? `?${params.join('&')}` : '');
};
export const createAgentMemory = client.create;
export const updateAgentMemory = client.update;
export const deleteAgentMemory = client.remove;
