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

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

export const contentStatusValidator = v.union(
  v.literal("DRAFT"),
  v.literal("PUBLISHED"),
);

export const blogStatusValidator = v.union(
  v.literal("draft"),
  v.literal("published"),
  v.literal("archived"),
);

export const announcementTypeValidator = v.union(
  v.literal("BANNER"),
  v.literal("MODAL"),
  v.literal("INLINE"),
);

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

export const contentTables = {
  blog_posts: defineTable({
    prismaId: v.optional(v.string()),
    title: v.string(),
    titleAr: v.optional(v.string()),
    slug: v.string(),
    content: v.string(),
    contentAr: v.optional(v.string()),
    excerpt: v.optional(v.string()),
    excerptAr: v.optional(v.string()),
    language: v.optional(v.string()),
    // Legacy alias — some queries use "lang" instead of "language"
    lang: v.optional(v.string()),
    authorId: v.optional(v.string()),
    authorName: v.optional(v.string()),
    // Legacy alias — some queries use "author" string
    author: v.optional(v.string()),
    status: v.optional(blogStatusValidator),
    // Legacy alias — some queries filter by "published" boolean
    published: v.optional(v.boolean()),
    tags: v.array(v.string()),
    coverImageStorageId: v.optional(v.id("_storage")),
    coverImage: v.optional(v.string()),
    featured: v.boolean(),
    featuredOrder: v.optional(v.number()),
    viewCount: v.optional(v.number()),
    metaTitle: v.optional(v.string()),
    metaDescription: v.optional(v.string()),
    metaKeywords: v.optional(v.array(v.string())),
    ogImage: v.optional(v.string()),
    readingTimeMinutes: v.optional(v.number()),
    date: v.optional(v.string()),
    publishedAt: v.optional(v.number()),
    createdAt: v.number(),
    updatedAt: v.number(),
  })
    .index("by_slug", ["slug"])
    .index("by_status", ["status"])
    .index("by_status_language", ["status", "language"])
    .index("by_authorId", ["authorId"])
    .index("by_featured", ["featured"])
    .index("by_language_status", ["language", "status"])
    .index("by_createdAt", ["createdAt"])
    .index("by_publishedAt", ["publishedAt"])
    .index("by_slug_lang", ["slug", "lang"])
    .index("by_lang_published", ["lang", "published"]),

  hero_sections: defineTable({
    prismaId: v.optional(v.string()),
    lang: v.string(),
    status: contentStatusValidator,
    version: v.number(),
    eyebrow: v.optional(v.string()),
    title: v.string(),
    subtitle: v.optional(v.string()),
    primaryCta: v.optional(v.any()),
    secondaryCta: v.optional(v.any()),
    badgeText: v.optional(v.string()),
    alignment: v.string(),
    background: v.optional(v.any()),
    mediaId: v.optional(v.string()),
    createdBy: v.optional(v.string()),
    updatedBy: v.optional(v.string()),
    publishedBy: v.optional(v.string()),
    createdAt: v.number(),
    updatedAt: v.number(),
    publishedAt: v.optional(v.number()),
  }).index("by_lang_status", ["lang", "status"]),

  feature_blocks: defineTable({
    prismaId: v.optional(v.string()),
    lang: v.string(),
    status: contentStatusValidator,
    version: v.number(),
    title: v.optional(v.string()),
    subtitle: v.optional(v.string()),
    layout: v.string(),
    items: v.any(),
    createdBy: v.optional(v.string()),
    updatedBy: v.optional(v.string()),
    publishedBy: v.optional(v.string()),
    createdAt: v.number(),
    updatedAt: v.number(),
    publishedAt: v.optional(v.number()),
  }).index("by_lang_status", ["lang", "status"]),

  footer_blocks: defineTable({
    prismaId: v.optional(v.string()),
    lang: v.string(),
    status: contentStatusValidator,
    version: v.number(),
    columns: v.any(),
    legalLinks: v.any(),
    contactText: v.optional(v.string()),
    newsletter: v.boolean(),
    socialLinks: v.any(),
    createdBy: v.optional(v.string()),
    updatedBy: v.optional(v.string()),
    publishedBy: v.optional(v.string()),
    createdAt: v.number(),
    updatedAt: v.number(),
    publishedAt: v.optional(v.number()),
  }).index("by_lang_status", ["lang", "status"]),

  cta_sections: defineTable({
    prismaId: v.optional(v.string()),
    lang: v.string(),
    status: contentStatusValidator,
    version: v.number(),
    title: v.optional(v.string()),
    subtitle: v.optional(v.string()),
    ctaLabel: v.optional(v.string()),
    ctaHref: v.optional(v.string()),
    background: v.optional(v.any()),
    createdBy: v.optional(v.string()),
    updatedBy: v.optional(v.string()),
    publishedBy: v.optional(v.string()),
    createdAt: v.number(),
    updatedAt: v.number(),
    publishedAt: v.optional(v.number()),
  }).index("by_lang_status", ["lang", "status"]),

  faq_entries: defineTable({
    prismaId: v.optional(v.string()),
    lang: v.string(),
    status: contentStatusValidator,
    version: v.number(),
    entries: v.any(),
    createdBy: v.optional(v.string()),
    updatedBy: v.optional(v.string()),
    publishedBy: v.optional(v.string()),
    createdAt: v.number(),
    updatedAt: v.number(),
    publishedAt: v.optional(v.number()),
  }).index("by_lang_status", ["lang", "status"]),

  announcements: defineTable({
    prismaId: v.optional(v.string()),
    lang: v.string(),
    status: contentStatusValidator,
    version: v.number(),
    variant: announcementTypeValidator,
    title: v.string(),
    body: v.optional(v.string()),
    ctaLabel: v.optional(v.string()),
    ctaHref: v.optional(v.string()),
    targetPages: v.array(v.string()),
    targetRoles: v.array(v.string()),
    startAt: v.optional(v.number()),
    endAt: v.optional(v.number()),
    dismissible: v.boolean(),
    createdBy: v.optional(v.string()),
    updatedBy: v.optional(v.string()),
    publishedBy: v.optional(v.string()),
    createdAt: v.number(),
    updatedAt: v.number(),
    publishedAt: v.optional(v.number()),
  })
    .index("by_endAt", ["endAt"])
    .index("by_lang_status", ["lang", "status"])
    .index("by_startAt", ["startAt"]),

  page_layouts: defineTable({
    prismaId: v.optional(v.string()),
    lang: v.string(),
    status: contentStatusValidator,
    version: v.number(),
    sections: v.any(),
    createdBy: v.optional(v.string()),
    updatedBy: v.optional(v.string()),
    publishedBy: v.optional(v.string()),
    createdAt: v.number(),
    updatedAt: v.number(),
    publishedAt: v.optional(v.number()),
  }).index("by_lang_status", ["lang", "status"]),

  site_settings: defineTable({
    prismaId: v.optional(v.string()),
    lang: v.string(),
    status: contentStatusValidator,
    version: v.number(),
    theme: v.any(),
    branding: v.any(),
    createdBy: v.optional(v.string()),
    updatedBy: v.optional(v.string()),
    publishedBy: v.optional(v.string()),
    createdAt: v.number(),
    updatedAt: v.number(),
    publishedAt: v.optional(v.number()),
  }).index("by_lang_status", ["lang", "status"]),

  social_proof: defineTable({
    prismaId: v.optional(v.string()),
    lang: v.string(),
    status: contentStatusValidator,
    version: v.number(),
    title: v.optional(v.string()),
    subtitle: v.optional(v.string()),
    logos: v.any(),
    testimonials: v.any(),
    createdBy: v.optional(v.string()),
    updatedBy: v.optional(v.string()),
    publishedBy: v.optional(v.string()),
    createdAt: v.number(),
    updatedAt: v.number(),
    publishedAt: v.optional(v.number()),
  }).index("by_lang_status", ["lang", "status"]),

  content_versions: defineTable({
    prismaId: v.optional(v.string()),
    entityType: v.string(),
    entityId: v.string(),
    version: v.number(),
    snapshot: v.any(),
    createdBy: v.optional(v.string()),
    createdAt: v.number(),
  }).index("by_entityType_entityId_version", [
    "entityType",
    "entityId",
    "version",
  ]),

  preview_tokens: defineTable({
    prismaId: v.optional(v.string()),
    token: v.string(),
    scopes: v.any(),
    expiresAt: v.number(),
    createdBy: v.optional(v.string()),
    createdAt: v.number(),
  })
    .index("by_expiresAt", ["expiresAt"])
    .index("by_token", ["token"]),

  media_assets: defineTable({
    prismaId: v.optional(v.string()),
    url: v.string(),
    alt: v.string(),
    type: v.string(),
    width: v.optional(v.number()),
    height: v.optional(v.number()),
    tags: v.array(v.string()),
    uploadedBy: v.optional(v.string()),
    createdAt: v.number(),
  })
    .index("by_createdAt", ["createdAt"])
    .index("by_type", ["type"]),

  events: defineTable({
    prismaId: v.optional(v.string()),
    lang: v.string(),
    status: contentStatusValidator,
    version: v.number(),
    title: v.string(),
    description: v.optional(v.string()),
    startAt: v.number(),
    endAt: v.optional(v.number()),
    location: v.optional(v.string()),
    ctaLabel: v.optional(v.string()),
    ctaHref: v.optional(v.string()),
    heroImage: v.optional(v.string()),
    tags: v.array(v.string()),
    audience: v.optional(v.string()),
    publishToSite: v.boolean(),
    publishToSocial: v.boolean(),
    campaignId: v.optional(v.string()),
    createdBy: v.optional(v.string()),
    updatedBy: v.optional(v.string()),
    publishedBy: v.optional(v.string()),
    createdAt: v.number(),
    updatedAt: v.number(),
    publishedAt: v.optional(v.number()),
  })
    .index("by_lang_status", ["lang", "status"])
    .index("by_publishToSite", ["publishToSite"])
    .index("by_startAt", ["startAt"]),

  campaigns: defineTable({
    prismaId: v.optional(v.string()),
    name: v.string(),
    utmSource: v.optional(v.string()),
    utmMedium: v.optional(v.string()),
    utmCampaign: v.optional(v.string()),
    utmContent: v.optional(v.string()),
    landingPage: v.optional(v.string()),
    kpis: v.optional(v.any()),
    createdBy: v.optional(v.string()),
    createdAt: v.number(),
  }).index("by_name", ["name"]),
};
