import { pgTable, text, timestamp, index, uuid, boolean, integer, jsonb } from "drizzle-orm/pg-core";
import { subscriptionTier } from "./enums";

export const waitlistEntries = pgTable("waitlist_entries", {
  id: uuid("id").defaultRandom().primaryKey(),
  email: text("email").notNull(),
  name: text("name"),
  source: text("source"),
  tier: subscriptionTier("tier"),
  utmSource: text("utm_source"),
  utmMedium: text("utm_medium"),
  utmCampaign: text("utm_campaign"),
  metadata: jsonb("metadata"),
  convertedAt: timestamp("converted_at", { withTimezone: true, mode: "date" }),
  convertedToUserId: text("converted_to_user_id"),
  convertedToSubscriptionId: text("converted_to_subscription_id"),
  createdAt: timestamp("created_at", { withTimezone: true, mode: "date" }).defaultNow().notNull(),
  updatedAt: timestamp("updated_at", { withTimezone: true, mode: "date" }).defaultNow().notNull().$onUpdate(() => new Date()),
}, (table) => ({
  emailIdx: index("waitlist_entries_email_idx").on(table.email),
  sourceIdx: index("waitlist_entries_source_idx").on(table.source),
  createdAtIdx: index("waitlist_entries_created_at_idx").on(table.createdAt),
}));

export const blogPosts = pgTable("blog_posts", {
  id: uuid("id").defaultRandom().primaryKey(),
  slug: text("slug").notNull().unique(),
  title: text("title").notNull(),
  excerpt: text("excerpt"),
  content: text("content").notNull(),
  authorName: text("author_name"),
  coverImageUrl: text("cover_image_url"),
  tags: text("tags").array().notNull(),
  published: boolean("published").default(false).notNull(),
  publishedAt: timestamp("published_at", { withTimezone: true, mode: "date" }),
  viewCount: integer("view_count").default(0).notNull(),
  createdAt: timestamp("created_at", { withTimezone: true, mode: "date" }).defaultNow().notNull(),
  updatedAt: timestamp("updated_at", { withTimezone: true, mode: "date" }).defaultNow().notNull().$onUpdate(() => new Date()),
}, (table) => ({
  slugIdx: index("blog_posts_slug_idx").on(table.slug),
  publishedIdx: index("blog_posts_published_idx").on(table.published, table.publishedAt),
  tagsIdx: index("blog_posts_tags_idx").on(table.tags),
}));
