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

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

export const notificationTypeValidator = v.union(
  v.literal("new_booking"),
  v.literal("deposit_received"),
  v.literal("new_conversation"),
  v.literal("booking_reminder"),
  v.literal("system_alert"),
  v.literal("integration_error"),
  v.literal("new_lead"),
);

export const alertSeverityValidator = v.union(
  v.literal("INFO"),
  v.literal("WARNING"),
  v.literal("ERROR"),
  v.literal("CRITICAL"),
);

export const alertTypeValidator = v.union(
  v.literal("NEW_DEMO"),
  v.literal("NEW_CONTACT"),
  v.literal("NEW_USER"),
  v.literal("PAYMENT_FAILED"),
  v.literal("INTEGRATION_ERROR"),
  v.literal("SYSTEM_ERROR"),
);

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

export const notificationTables = {
  notifications: defineTable({
    prismaId: v.optional(v.string()),
    userId: v.string(),
    type: notificationTypeValidator,
    title: v.string(),
    message: v.string(),
    actionUrl: v.optional(v.string()),
    isRead: v.boolean(),
    readAt: v.optional(v.number()),
    metadata: v.optional(v.any()),
    createdAt: v.number(),
  })
    .index("by_createdAt", ["createdAt"])
    .index("by_isRead", ["isRead"])
    .index("by_userId", ["userId"])
    .index("by_userId_isRead", ["userId", "isRead"])
    .index("by_userId_createdAt", ["userId", "createdAt"])
    .index("by_userId_type_isRead", ["userId", "type", "isRead"]),

  admin_notifications: defineTable({
    prismaId: v.optional(v.string()),
    type: v.string(),
    title: v.string(),
    message: v.string(),
    metadata: v.any(),
    isRead: v.boolean(),
    readAt: v.optional(v.number()),
    readBy: v.optional(v.string()),
    createdAt: v.number(),
  })
    .index("by_createdAt", ["createdAt"])
    .index("by_isRead", ["isRead"])
    .index("by_type", ["type"]),

  system_messages: defineTable({
    prismaId: v.optional(v.string()),
    type: v.string(),
    title: v.optional(v.string()),
    message: v.string(),
    isActive: v.boolean(),
    createdAt: v.number(),
    updatedAt: v.number(),
    createdBy: v.string(),
    expiresAt: v.optional(v.number()),
  })
    .index("by_createdBy", ["createdBy"])
    .index("by_isActive", ["isActive"]),

  system_message_views: defineTable({
    prismaId: v.optional(v.string()),
    messageId: v.string(),
    userId: v.optional(v.string()),
    viewedAt: v.number(),
  })
    .index("by_messageId", ["messageId"])
    .index("by_userId", ["userId"])
    .index("by_messageId_userId", ["messageId", "userId"]),

  staff_alerts: defineTable({
    prismaId: v.optional(v.string()),
    type: alertTypeValidator,
    title: v.string(),
    message: v.string(),
    severity: alertSeverityValidator,
    isRead: v.boolean(),
    readAt: v.optional(v.number()),
    readBy: v.optional(v.string()),
    metadata: v.optional(v.any()),
    createdAt: v.number(),
  })
    .index("by_createdAt", ["createdAt"])
    .index("by_isRead", ["isRead"])
    .index("by_type", ["type"]),
};
