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

// ── Enums ──────────────────────────────────────────────────────────────

export const nurtureSequenceTypeValidator = v.union(
  v.literal("TRIAL_ONBOARDING"),
  v.literal("ABANDONED_DEMO"),
  v.literal("POST_SIGNUP"),
  v.literal("PRODUCT_EDUCATION"),
  v.literal("CONVERSION"),
);

export const nurtureEmailStatusValidator = v.union(
  v.literal("PENDING"),
  v.literal("SCHEDULED"),
  v.literal("SENT"),
  v.literal("OPENED"),
  v.literal("CLICKED"),
  v.literal("FAILED"),
  v.literal("SKIPPED"),
);

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

export const nurtureTables = {
  email_nurture_sequences: defineTable({
    prismaId: v.optional(v.string()),
    name: v.string(),
    nameAr: v.optional(v.string()),
    description: v.optional(v.string()),
    sequenceType: nurtureSequenceTypeValidator,
    isActive: v.boolean(),
    triggerEvent: v.string(),
    triggerConditions: v.optional(v.any()),
    createdBy: v.optional(v.string()),
    updatedBy: v.optional(v.string()),
    createdAt: v.number(),
    updatedAt: v.number(),
  })
    .index("by_sequenceType", ["sequenceType"])
    .index("by_isActive", ["isActive"])
    .index("by_triggerEvent", ["triggerEvent"]),

  email_nurture_sequence_steps: defineTable({
    prismaId: v.optional(v.string()),
    sequenceId: v.string(),
    stepNumber: v.number(),
    name: v.string(),
    subjectEn: v.string(),
    subjectAr: v.string(),
    delayHours: v.number(),
    templateKey: v.string(),
    isActive: v.boolean(),
    createdAt: v.number(),
    updatedAt: v.number(),
  })
    .index("by_sequenceId", ["sequenceId"])
    .index("by_isActive", ["isActive"])
    .index("by_sequenceId_stepNumber", ["sequenceId", "stepNumber"]),

  user_nurture_sequence_states: defineTable({
    prismaId: v.optional(v.string()),
    userId: v.string(),
    sequenceId: v.string(),
    currentStep: v.number(),
    status: v.string(),
    startedAt: v.number(),
    completedAt: v.optional(v.number()),
    pausedAt: v.optional(v.number()),
    cancelledAt: v.optional(v.number()),
    metadata: v.optional(v.any()),
  })
    .index("by_userId", ["userId"])
    .index("by_sequenceId", ["sequenceId"])
    .index("by_status", ["status"])
    .index("by_userId_sequenceId", ["userId", "sequenceId"]),

  user_nurture_sequence_emails: defineTable({
    prismaId: v.optional(v.string()),
    stateId: v.string(),
    stepId: v.string(),
    userId: v.string(),
    email: v.string(),
    status: nurtureEmailStatusValidator,
    scheduledFor: v.number(),
    sentAt: v.optional(v.number()),
    openedAt: v.optional(v.number()),
    clickedAt: v.optional(v.number()),
    failedAt: v.optional(v.number()),
    errorMessage: v.optional(v.string()),
    metadata: v.optional(v.any()),
    createdAt: v.number(),
    updatedAt: v.number(),
  })
    .index("by_userId", ["userId"])
    .index("by_stateId", ["stateId"])
    .index("by_stepId", ["stepId"])
    .index("by_status", ["status"])
    .index("by_scheduledFor", ["scheduledFor"])
    .index("by_email", ["email"]),
};
