/**
 * Dossier Domain Schema
 * 
 * Tables for bidirectional focus sync between Fast Agent Panel and Dossier views.
 * Enables agent-orchestrated state management for:
 * - Focus state (text↔chart highlighting)
 * - Annotations (agent-generated chart labels)
 * - Enrichment (cached context for data points)
 */

import { defineTable } from "convex/server";
import { v } from "convex/values";

/* ------------------------------------------------------------------ */
/* DOSSIER FOCUS STATE - Real-time focus sync between views            */
/* ------------------------------------------------------------------ */
export const dossierFocusState = defineTable({
  /** User ID for multi-user isolation */
  userId: v.id("users"),
  /** Brief/memory ID being viewed */
  briefId: v.string(),
  /** Current act (scrolly section) */
  currentAct: v.optional(v.union(
    v.literal("actI"),
    v.literal("actII"),
    v.literal("actIII"),
  )),
  /** Focused data point index on chart */
  focusedDataIndex: v.optional(v.number()),
  /** Hovered interactive span ID in text */
  hoveredSpanId: v.optional(v.string()),
  /** Active section ID in scrolly layout */
  activeSectionId: v.optional(v.string()),
  /** Chart series currently focused */
  focusedSeriesId: v.optional(v.string()),
  /** Source of last focus change */
  focusSource: v.optional(v.union(
    v.literal("chart_hover"),
    v.literal("text_hover"),
    v.literal("agent_tool"),
    v.literal("panel_action"),
  )),
  /** Timestamp of last update */
  updatedAt: v.number(),
})
  .index("by_user_brief", ["userId", "briefId"])
  .index("by_user", ["userId"]);

/* ------------------------------------------------------------------ */
/* DOSSIER ANNOTATIONS - Agent-generated chart annotations            */
/* ------------------------------------------------------------------ */
export const dossierAnnotations = defineTable({
  /** User ID for multi-user isolation */
  userId: v.id("users"),
  /** Brief/memory ID */
  briefId: v.string(),
  /** Chart series this annotation belongs to */
  seriesId: v.optional(v.string()),
  /** Data point index this annotates */
  dataIndex: v.number(),
  /** Annotation text */
  text: v.string(),
  /** Position relative to data point */
  position: v.union(
    v.literal("above"),
    v.literal("below"),
    v.literal("left"),
    v.literal("right"),
  ),
  /** Optional icon */
  icon: v.optional(v.string()),
  /** Which acts this annotation is visible in */
  visibleInActs: v.array(v.union(
    v.literal("actI"),
    v.literal("actII"),
    v.literal("actIII"),
  )),
  /** Was this generated by agent or user? */
  source: v.union(
    v.literal("agent"),
    v.literal("user"),
    v.literal("system"),
  ),
  /** Agent thread that created this (if agent source) */
  agentThreadId: v.optional(v.string()),
  /** Creation timestamp */
  createdAt: v.number(),
})
  .index("by_user_brief", ["userId", "briefId"])
  .index("by_brief_series", ["briefId", "seriesId"])
  .index("by_brief_dataIndex", ["briefId", "dataIndex"]);

/* ------------------------------------------------------------------ */
/* DOSSIER ENRICHMENT - Cached context for data points                */
/* ------------------------------------------------------------------ */
export const dossierEnrichment = defineTable({
  /** User ID for multi-user isolation */
  userId: v.id("users"),
  /** Brief/memory ID */
  briefId: v.string(),
  /** Chart series */
  seriesId: v.optional(v.string()),
  /** Data point index */
  dataIndex: v.number(),
  /** Enrichment title */
  title: v.string(),
  /** Detailed context from agent */
  context: v.string(),
  /** Related entities (companies, people, events) */
  entities: v.optional(v.array(v.object({
    name: v.string(),
    type: v.string(),
    url: v.optional(v.string()),
  }))),
  /** Source URLs for citations */
  sources: v.optional(v.array(v.object({
    url: v.string(),
    title: v.optional(v.string()),
    retrievedAt: v.number(),
  }))),
  /** Agent thread that created this */
  agentThreadId: v.optional(v.string()),
  /** Cache expiry (ms since epoch) */
  expiresAt: v.optional(v.number()),
  /** Creation timestamp */
  createdAt: v.number(),
  /** Last access timestamp (for LRU eviction) */
  lastAccessedAt: v.number(),
})
  .index("by_user_brief", ["userId", "briefId"])
  .index("by_brief_dataIndex", ["briefId", "dataIndex"])
  .index("by_expires", ["expiresAt"]);

