import {
  pgTable,
  text,
  timestamp,
  uuid,
  varchar,
  check,
} from "drizzle-orm/pg-core";
import { sql } from "drizzle-orm";

// ---------------------------------------------------------------------------
// Support schema — aligned to PRODUCTION as of 2026-05-19 baseline.
// Tables: support_tickets, support_responses (legacy 0007_support_tickets).
// ---------------------------------------------------------------------------

export const supportTickets = pgTable(
  "support_tickets",
  {
    id: uuid("id").primaryKey().defaultRandom(),
    familyId: uuid("family_id"),
    userId: uuid("user_id"),
    email: varchar("email", { length: 255 }).notNull(),
    subject: varchar("subject", { length: 255 }).notNull(),
    description: text("description"),
    status: varchar("status", { length: 20 }).default("open"),
    priority: varchar("priority", { length: 20 }).default("normal"),
    createdAt: timestamp("created_at", { withTimezone: true }).defaultNow(),
    updatedAt: timestamp("updated_at", { withTimezone: true }).defaultNow(),
  },
  (table) => [
    check(
      "support_tickets_status_check",
      sql`(status)::text = ANY (ARRAY['open','in_progress','resolved','closed'])`
    ),
    check(
      "support_tickets_priority_check",
      sql`(priority)::text = ANY (ARRAY['low','normal','high','urgent'])`
    ),
  ]
);

export const supportResponses = pgTable("support_responses", {
  id: uuid("id").primaryKey().defaultRandom(),
  ticketId: uuid("ticket_id"),
  adminId: uuid("admin_id"),
  message: text("message").notNull(),
  createdAt: timestamp("created_at", { withTimezone: true }).defaultNow(),
});

export type SupportTicket = typeof supportTickets.$inferSelect;
export type SupportResponse = typeof supportResponses.$inferSelect;
