import { pgTable, text, timestamp, boolean, primaryKey } from "drizzle-orm/pg-core";
import { relations } from "drizzle-orm";
import { users, tierEnum } from "./users";

/** Absence of a row for a (featureKey, tier) pair means "enabled" — rows only
 *  exist to record an override, so adding a new gated feature never requires
 *  a backfill. */
export const featureFlags = pgTable("feature_flags", {
  featureKey: text("feature_key").notNull(),
  tier: tierEnum("tier").notNull(),
  enabled: boolean("enabled").notNull().default(true),
  updatedAt: timestamp("updated_at").notNull().defaultNow(),
  updatedById: text("updated_by_id").references(() => users.id, { onDelete: "set null" }),
}, (t) => [
  primaryKey({ columns: [t.featureKey, t.tier] }),
]);

export const featureFlagsRelations = relations(featureFlags, ({ one }) => ({
  updatedBy: one(users, { fields: [featureFlags.updatedById], references: [users.id] }),
}));
