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

// ── Tables ─────────────────────────────────────────────────────────────

export const analyticsTables = {
  analytics_events: defineTable({
    prismaId: v.optional(v.string()),
    eventName: v.string(),
    category: v.string(),
    properties: v.optional(v.any()),
    userId: v.optional(v.string()),
    sessionId: v.optional(v.string()),
    page: v.optional(v.string()),
    createdAt: v.number(),
  })
    .index("by_eventName", ["eventName"])
    .index("by_category", ["category"])
    .index("by_userId", ["userId"])
    .index("by_sessionId", ["sessionId"])
    .index("by_createdAt", ["createdAt"])
    .index("by_userId_createdAt", ["userId", "createdAt"])
    .index("by_category_createdAt", ["category", "createdAt"])
    .index("by_eventName_createdAt", ["eventName", "createdAt"]),

  site_visits: defineTable({
    prismaId: v.optional(v.string()),
    visitorId: v.string(),
    sessionId: v.string(),
    page: v.string(),
    country: v.optional(v.string()),
    city: v.optional(v.string()),
    language: v.optional(v.string()),
    userAgent: v.optional(v.string()),
    referrer: v.optional(v.string()),
    userId: v.optional(v.string()),
    createdAt: v.number(),
  })
    .index("by_createdAt", ["createdAt"])
    .index("by_page", ["page"])
    .index("by_sessionId", ["sessionId"])
    .index("by_userId", ["userId"])
    .index("by_visitorId", ["visitorId"]),

  workflow_analytics: defineTable({
    prismaId: v.optional(v.string()),
    userId: v.string(),
    workflowType: v.string(),
    date: v.number(),
    messagesReceived: v.number(),
    messagesSent: v.number(),
    appointmentsBooked: v.number(),
    quotationsSent: v.number(),
    leadsGenerated: v.number(),
    metadata: v.optional(v.any()),
  })
    .index("by_userId_date", ["userId", "date"])
    .index("by_userId_workflowType_date", ["userId", "workflowType", "date"]),

  web_vitals: defineTable({
    prismaId: v.optional(v.string()),
    name: v.string(),
    value: v.number(),
    rating: v.union(
      v.literal("good"),
      v.literal("needs-improvement"),
      v.literal("poor"),
    ),
    url: v.string(),
    userAgent: v.optional(v.string()),
    navigationType: v.optional(v.string()),
    recordedAt: v.number(),
  })
    .index("by_name", ["name"])
    .index("by_recordedAt", ["recordedAt"])
    .index("by_url", ["url"])
    .index("by_name_recordedAt", ["name", "recordedAt"]),

  workflow_errors: defineTable({
    prismaId: v.optional(v.string()),
    userId: v.optional(v.string()),
    workflowId: v.optional(v.string()),
    workflowName: v.optional(v.string()),
    executionId: v.optional(v.string()),
    errorType: v.string(),
    errorCode: v.optional(v.string()),
    errorMessage: v.string(),
    errorStack: v.optional(v.string()),
    service: v.optional(v.string()),
    severity: v.string(),
    context: v.any(),
    resolvedAt: v.optional(v.number()),
    resolvedBy: v.optional(v.string()),
    createdAt: v.number(),
  })
    .index("by_createdAt", ["createdAt"])
    .index("by_errorType", ["errorType"])
    .index("by_severity", ["severity"])
    .index("by_userId_createdAt", ["userId", "createdAt"])
    .index("by_userId", ["userId"])
    .index("by_workflowId", ["workflowId"]),
};
