import {
  boolean,
  integer,
  jsonb,
  pgTable,
  serial,
  text,
  timestamp,
  varchar,
} from "drizzle-orm/pg-core";

/* ------------------------------------------------------------------ */
/* Projects                                                            */
/* ------------------------------------------------------------------ */

export const projects = pgTable("projects", {
  id: serial("id").primaryKey(),
  slug: varchar("slug", { length: 120 }).notNull().unique(),
  title: varchar("title", { length: 160 }).notNull(),
  category: varchar("category", { length: 80 }).notNull(),
  year: varchar("year", { length: 16 }).notNull(),
  location: varchar("location", { length: 160 }),
  cover: varchar("cover", { length: 300 }).notNull(),
  summary: text("summary").notNull(),
  description: text("description").notNull(),
  specs: jsonb("specs").$type<string[]>().notNull().default([]),
  awards: jsonb("awards").$type<{ label: string; url?: string }[]>()
    .notNull()
    .default([]),
  credits: jsonb("credits").$type<{ role: string; value: string; url?: string }[]>()
    .notNull()
    .default([]),
  images: jsonb("images").$type<string[]>().notNull().default([]),
  videoUrl: text("video_url"),
  buyUrl: text("buy_url"),
  buyLabel: varchar("buy_label", { length: 80 }),
  externalLinks: jsonb("external_links")
    .$type<{ label: string; url: string }[]>()
    .notNull()
    .default([]),
  keywords: jsonb("keywords").$type<string[]>().notNull().default([]),
  featured: boolean("featured").notNull().default(false),
  sortOrder: integer("sort_order").notNull().default(0),
  createdAt: timestamp("created_at", { withTimezone: true })
    .notNull()
    .defaultNow(),
});

/* ------------------------------------------------------------------ */
/* Awards                                                              */
/* ------------------------------------------------------------------ */

export const awards = pgTable("awards", {
  id: serial("id").primaryKey(),
  label: varchar("label", { length: 220 }).notNull(),
  url: text("url"),
  sortOrder: integer("sort_order").notNull().default(0),
});

/* ------------------------------------------------------------------ */
/* Exhibitions                                                         */
/* ------------------------------------------------------------------ */

export const exhibitions = pgTable("exhibitions", {
  id: serial("id").primaryKey(),
  label: varchar("label", { length: 220 }).notNull(),
  sortOrder: integer("sort_order").notNull().default(0),
});

/* ------------------------------------------------------------------ */
/* Clients                                                             */
/* ------------------------------------------------------------------ */

export const clients = pgTable("clients", {
  id: serial("id").primaryKey(),
  label: varchar("label", { length: 160 }).notNull(),
  url: text("url"),
  sortOrder: integer("sort_order").notNull().default(0),
});

/* ------------------------------------------------------------------ */
/* Instagram cache – written by /api/instagram                         */
/* ------------------------------------------------------------------ */

export const instagramCache = pgTable("instagram_cache", {
  key: varchar("key", { length: 64 }).primaryKey(),
  payload: jsonb("payload").$type<unknown>().notNull(),
  source: varchar("source", { length: 64 }).notNull(),
  fetchedAt: timestamp("fetched_at", { withTimezone: true })
    .notNull()
    .defaultNow(),
});

/* ------------------------------------------------------------------ */
/* Contact / enquiry log                                               */
/* ------------------------------------------------------------------ */

export const enquiries = pgTable("enquiries", {
  id: serial("id").primaryKey(),
  name: varchar("name", { length: 160 }).notNull(),
  email: varchar("email", { length: 200 }).notNull(),
  company: varchar("company", { length: 200 }),
  subject: varchar("subject", { length: 220 }).notNull(),
  message: text("message").notNull(),
  createdAt: timestamp("created_at", { withTimezone: true })
    .notNull()
    .defaultNow(),
});

export type Project = typeof projects.$inferSelect;
export type Award = typeof awards.$inferSelect;
export type Exhibition = typeof exhibitions.$inferSelect;
export type Client = typeof clients.$inferSelect;
export type Enquiry = typeof enquiries.$inferSelect;
