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

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

export const ticketCategoryValidator = v.union(
  v.literal("BILLING"),
  v.literal("TECHNICAL"),
  v.literal("FEATURE_REQUEST"),
  v.literal("OTHER"),
);

export const ticketPriorityValidator = v.union(
  v.literal("LOW"),
  v.literal("NORMAL"),
  v.literal("URGENT"),
);

export const ticketStatusValidator = v.union(
  v.literal("OPEN"),
  v.literal("IN_PROGRESS"),
  v.literal("RESOLVED"),
  v.literal("CLOSED"),
);

export const ticketSenderTypeValidator = v.union(
  v.literal("USER"),
  v.literal("STAFF"),
);

export const supportFAQCategoryValidator = v.union(
  v.literal("GETTING_STARTED"),
  v.literal("BILLING"),
  v.literal("TECHNICAL"),
  v.literal("WHATSAPP"),
  v.literal("APPOINTMENTS"),
);

export const emailStatusValidator = v.union(
  v.literal("DRAFT"),
  v.literal("SENT"),
  v.literal("OPENED"),
  v.literal("PAID"),
  v.literal("OVERDUE"),
  v.literal("CANCELLED"),
);

export const contactMessageDirectionValidator = v.union(
  v.literal("outbound"),
  v.literal("inbound"),
);

export const contactMessageStatusValidator = v.union(
  v.literal("draft"),
  v.literal("queued"),
  v.literal("sent"),
  v.literal("failed"),
);

export const contactStatusValidator = v.union(
  v.literal("NEW"),
  v.literal("READ"),
  v.literal("IN_PROGRESS"),
  v.literal("RESPONDED"),
  v.literal("CLOSED"),
);

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

export const supportTables = {
  support_tickets: defineTable({
    prismaId: v.optional(v.string()),
    userId: v.string(),
    subject: v.string(),
    message: v.string(),
    attachments: v.optional(v.any()),
    resolvedAt: v.optional(v.number()),
    resolvedBy: v.optional(v.string()),
    resolution: v.optional(v.string()),
    createdAt: v.number(),
    updatedAt: v.number(),
    category: ticketCategoryValidator,
    priority: ticketPriorityValidator,
    status: ticketStatusValidator,
  })
    .index("by_category", ["category"])
    .index("by_createdAt", ["createdAt"])
    .index("by_priority", ["priority"])
    .index("by_status", ["status"])
    .index("by_userId", ["userId"]),

  ticket_messages: defineTable({
    prismaId: v.optional(v.string()),
    ticketId: v.string(),
    senderId: v.string(),
    senderType: ticketSenderTypeValidator,
    message: v.string(),
    attachments: v.optional(v.any()),
    isRead: v.boolean(),
    readAt: v.optional(v.number()),
    createdAt: v.number(),
  })
    .index("by_isRead", ["isRead"])
    .index("by_senderId", ["senderId"])
    .index("by_ticketId_createdAt", ["ticketId", "createdAt"]),

  support_faqs: defineTable({
    prismaId: v.optional(v.string()),
    category: supportFAQCategoryValidator,
    question: v.string(),
    questionAr: v.string(),
    answer: v.string(),
    answerAr: v.string(),
    order: v.number(),
    isPublished: v.boolean(),
    createdBy: v.optional(v.string()),
    updatedBy: v.optional(v.string()),
    createdAt: v.number(),
    updatedAt: v.number(),
  })
    .index("by_category_isPublished", ["category", "isPublished"])
    .index("by_isPublished", ["isPublished"])
    .index("by_order", ["order"]),

  contact_submissions: defineTable({
    prismaId: v.optional(v.string()),
    name: v.string(),
    email: v.string(),
    phone: v.string(),
    company: v.optional(v.string()),
    topic: v.string(),
    message: v.string(),
    preferred_language: v.string(),
    consent: v.boolean(),
    attachments: v.any(),
    ip_address: v.optional(v.string()),
    user_agent: v.optional(v.string()),
    submission_language: v.optional(v.string()),
    status: v.string(),
    resolved_at: v.optional(v.number()),
    resolved_by: v.optional(v.string()),
    notes: v.optional(v.string()),
    created_at: v.number(),
    updated_at: v.number(),
  })
    .index("by_created_at", ["created_at"])
    .index("by_email", ["email"])
    .index("by_status", ["status"]),

  contact_messages: defineTable({
    prismaId: v.optional(v.string()),
    contactId: v.string(),
    direction: contactMessageDirectionValidator,
    fromEmail: v.string(),
    toEmail: v.string(),
    ccEmails: v.any(),
    subject: v.optional(v.string()),
    bodyHtml: v.string(),
    bodyText: v.optional(v.string()),
    attachments: v.any(),
    sentBy: v.optional(v.string()),
    messageId: v.optional(v.string()),
    inReplyTo: v.optional(v.string()),
    status: contactMessageStatusValidator,
    createdAt: v.number(),
    updatedAt: v.number(),
  })
    .index("by_contactId", ["contactId"])
    .index("by_contactId_createdAt", ["contactId", "createdAt"])
    .index("by_sentBy", ["sentBy"])
    .index("by_status", ["status"])
    .index("by_createdAt", ["createdAt"]),

  email_templates: defineTable({
    prismaId: v.optional(v.string()),
    name: v.string(),
    subject: v.string(),
    bodyHtml: v.string(),
    createdBy: v.string(),
    isShared: v.boolean(),
    createdAt: v.number(),
    updatedAt: v.number(),
  })
    .index("by_createdBy", ["createdBy"])
    .index("by_isShared", ["isShared"])
    .index("by_createdAt", ["createdAt"]),

  billing_emails: defineTable({
    prismaId: v.optional(v.string()),
    userId: v.string(),
    staffId: v.string(),
    subject: v.string(),
    body: v.string(),
    amount: v.optional(v.number()),
    dueDate: v.optional(v.number()),
    status: emailStatusValidator,
    sentAt: v.number(),
    openedAt: v.optional(v.number()),
    paidAt: v.optional(v.number()),
  })
    .index("by_dueDate", ["dueDate"])
    .index("by_sentAt", ["sentAt"])
    .index("by_staffId", ["staffId"])
    .index("by_userId_status", ["userId", "status"]),
};
