import { pgTable, text, timestamp, uniqueIndex } from "drizzle-orm/pg-core";
import { intId, intPk } from "./columns";
import { users } from "./identity";

/**
 * Web Push (VAPID) subscriptions — Feature 7.3 / 12.4. One row per browser
 * (or PWA) endpoint. Distinct from session "devices" (refresh-token list).
 */
export const pushSubscriptions = pgTable(
  "push_subscriptions",
  {
    id: intPk(),
    userId: intId("user_id")
      .notNull()
      .references(() => users.id),
    endpoint: text("endpoint").notNull(),
    p256dh: text("p256dh").notNull(),
    auth: text("auth").notNull(),
    userAgent: text("user_agent"),
    createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
    lastSeenAt: timestamp("last_seen_at", { withTimezone: true }).notNull().defaultNow(),
  },
  (t) => [uniqueIndex("push_subscriptions_endpoint_idx").on(t.endpoint)],
);
