import { pgTable, text, timestamp, boolean, index, uniqueIndex } from "drizzle-orm/pg-core";
import { users } from "./users";
import { files } from "./files";

export const articles = pgTable(
  "articles",
  {
    id: text("id")
      .primaryKey()
      .$defaultFn(() => crypto.randomUUID()),
    slug: text("slug").notNull(),
    title: text("title").notNull(),
    excerpt: text("excerpt"),
    content: text("content"),
    image: text("image").references(() => files.id, { onDelete: "set null" }),
    tags: text("tags").array().default([]),
    publish_date: timestamp("publish_date").notNull().defaultNow(),
    author: text("author").references(() => users.id, { onDelete: "set null" }),
    category: text("category"),
    featured: boolean("featured").default(false),
    date_created: timestamp("date_created").notNull().defaultNow(),
    date_updated: timestamp("date_updated"),
  },
  (t) => [
    uniqueIndex("articles_slug_idx").on(t.slug),
    index("articles_publish_date_idx").on(t.publish_date),
    index("articles_featured_idx").on(t.featured),
  ],
);

export type Article = typeof articles.$inferSelect;
export type NewArticle = typeof articles.$inferInsert;
