import { defineSchema, defineTable } from "convex/server";
import { v } from "convex/values";
import { authTables } from "@convex-dev/auth/server";

export default defineSchema({
  ...authTables,

  acp_runtime_runs: defineTable({
    id: v.string(),
    user_id: v.string(),
    workspace_id: v.union(v.string(), v.null()),
    document_id: v.string(),
    session_id: v.string(),
    run_id: v.string(),
    title: v.union(v.string(), v.null()),
    profile: v.string(),
    acp_runtime: v.string(),
    source: v.literal("acp"),
    status: v.string(),
    trace_id: v.string(),
    runtime: v.any(),
    usage: v.any(),
    payload: v.any(),
    retention: v.any(),
    created_at: v.string(),
    updated_at: v.string(),
    deleted_at: v.union(v.string(), v.null()),
  })
    .index("by_run_id", ["run_id"])
    .index("by_user_workspace_document", ["user_id", "workspace_id", "document_id", "created_at"])
    .index("by_user_workspace_session", ["user_id", "workspace_id", "session_id", "created_at"])
    .index("by_user_created_at", ["user_id", "created_at"]),

  acp_runtime_events: defineTable({
    id: v.string(),
    user_id: v.string(),
    workspace_id: v.union(v.string(), v.null()),
    document_id: v.string(),
    session_id: v.string(),
    run_id: v.string(),
    profile: v.string(),
    acp_runtime: v.string(),
    source: v.literal("acp"),
    event_type: v.string(),
    payload: v.any(),
    created_at: v.string(),
  })
    .index("by_run_created_at", ["run_id", "created_at"])
    .index("by_user_workspace_session_created_at", [
      "user_id",
      "workspace_id",
      "session_id",
      "created_at",
    ]),
});
