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

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

export const reviewRequestStatusValidator = v.union(
  v.literal("PENDING"),
  v.literal("SENT"),
  v.literal("COMPLETED"),
  v.literal("EXPIRED"),
);

export const reviewReminderStatusValidator = v.union(
  v.literal("PENDING"),
  v.literal("SENT"),
  v.literal("FAILED"),
);

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

export const reviewTables = {
  reviews: defineTable({
    prismaId: v.optional(v.string()),
    userId: v.string(),
    customerName: v.string(),
    customerEmail: v.optional(v.string()),
    customerPhone: v.optional(v.string()),
    platform: v.string(),
    rating: v.number(),
    title: v.optional(v.string()),
    content: v.optional(v.string()),
    language: v.string(),
    isVerified: v.boolean(),
    responseText: v.optional(v.string()),
    respondedAt: v.optional(v.number()),
    reviewRequestId: v.optional(v.string()),
    questionResponses: v.optional(v.any()),
    isFeatured: v.boolean(),
    featuredAt: v.optional(v.number()),
    featuredOrder: v.optional(v.number()),
    createdAt: v.number(),
    updatedAt: v.number(),
  })
    .index("by_userId", ["userId"])
    .index("by_userId_rating", ["userId", "rating"])
    .index("by_userId_platform", ["userId", "platform"])
    .index("by_userId_isFeatured", ["userId", "isFeatured"])
    .index("by_createdAt", ["createdAt"]),

  review_requests: defineTable({
    prismaId: v.optional(v.string()),
    userId: v.string(),
    customerName: v.string(),
    customerEmail: v.optional(v.string()),
    customerPhone: v.optional(v.string()),
    channel: v.string(),
    token: v.string(),
    message: v.optional(v.string()),
    language: v.string(),
    status: reviewRequestStatusValidator,
    sentAt: v.optional(v.number()),
    completedAt: v.optional(v.number()),
    expiresAt: v.number(),
    bookingId: v.optional(v.string()),
    customerId: v.optional(v.string()),
    questions: v.optional(v.any()),
    source: v.string(),
    reminderCount: v.number(),
    lastReminderAt: v.optional(v.number()),
    nextReminderAt: v.optional(v.number()),
    reminderStatus: v.optional(reviewReminderStatusValidator),
    createdAt: v.number(),
    updatedAt: v.number(),
  })
    .index("by_userId", ["userId"])
    .index("by_token", ["token"])
    .index("by_status", ["status"])
    .index("by_expiresAt", ["expiresAt"])
    .index("by_bookingId", ["bookingId"])
    .index("by_customerId", ["customerId"])
    .index("by_source", ["source"])
    .index("by_status_nextReminderAt", ["status", "nextReminderAt"]),

  review_question_templates: defineTable({
    prismaId: v.optional(v.string()),
    userId: v.string(),
    organizationId: v.optional(v.string()),
    industry: v.optional(v.string()),
    // Per-question fields (granular mode)
    question: v.optional(v.string()),
    questionAr: v.optional(v.string()),
    questionType: v.optional(
      v.union(
        v.literal("rating"),
        v.literal("text"),
        v.literal("multiple_choice"),
        v.literal("yes_no"),
      ),
    ),
    isRequired: v.optional(v.boolean()),
    sortOrder: v.optional(v.number()),
    options: v.optional(v.array(v.string())),
    isActive: v.optional(v.boolean()),
    // Bulk questions field (legacy mode)
    questions: v.optional(v.any()),
    isDefault: v.optional(v.boolean()),
    createdAt: v.number(),
    updatedAt: v.number(),
  })
    .index("by_userId", ["userId"])
    .index("by_userId_industry", ["userId", "industry"])
    .index("by_organizationId", ["organizationId"])
    .index("by_userId_isActive", ["userId", "isActive"]),

  review_widget_settings: defineTable({
    prismaId: v.optional(v.string()),
    userId: v.string(),
    businessName: v.optional(v.string()),
    businessNameAr: v.optional(v.string()),
    logoUrl: v.optional(v.string()),
    primaryColor: v.string(),
    backgroundColor: v.string(),
    textColor: v.string(),
    thankYouMessage: v.optional(v.string()),
    thankYouMessageAr: v.optional(v.string()),
    showPoweredBy: v.boolean(),
    customCss: v.optional(v.string()),
    createdAt: v.number(),
    updatedAt: v.number(),
  }).index("by_userId", ["userId"]),

  referrals: defineTable({
    prismaId: v.optional(v.string()),
    userId: v.string(),
    code: v.string(),
    name: v.string(),
    description: v.optional(v.string()),
    rewardType: v.string(),
    rewardValue: v.number(),
    rewardCurrency: v.string(),
    maxUses: v.optional(v.number()),
    expiresAt: v.optional(v.number()),
    uses: v.number(),
    conversions: v.number(),
    rewardsIssued: v.number(),
    lastConvertedAt: v.optional(v.number()),
    lastRewardAt: v.optional(v.number()),
    status: v.string(),
    createdAt: v.number(),
    updatedAt: v.number(),
  })
    .index("by_userId", ["userId"])
    .index("by_code", ["code"])
    .index("by_status", ["status"])
    .index("by_userId_status", ["userId", "status"]),
};
