/**
 * Brand settings + brand features. Source: `db/migrations/0001_init.sql`.
 */
import {
  pgTable,
  uuid,
  text,
  jsonb,
  boolean,
  timestamp,
  uniqueIndex,
} from "drizzle-orm/pg-core";
import { brands } from "./brands";

export const brandSettings = pgTable(
  "brand_settings",
  {
    brandId: uuid("brand_id")
      .primaryKey()
      .references(() => brands.id, { onDelete: "cascade" }),
    legalBusinessName: text("legal_business_name"),
    phone: text("phone"),
    email: text("email"),
    websiteUrl: text("website_url"),
    streetAddress: text("street_address"),
    city: text("city"),
    state: text("state"),
    postalCode: text("postal_code"),
    country: text("country").default("US"),
    logoUrl: text("logo_url"),
    logoUrlDark: text("logo_url_dark"),
    heroImageUrl: text("hero_image_url"),
    tagline: text("tagline"),
    aboutHtml: text("about_html"),
    primaryColor: text("primary_color").default("#0F766E"),
    defaultEmailSignature: text("default_email_signature"),
    invoiceFooterNotes: text("invoice_footer_notes"),
    fromEmail: text("from_email"),
    fromName: text("from_name"),
    replyToEmail: text("reply_to_email"),
    customFooterText: text("custom_footer_text"),
    featureFlags: jsonb("feature_flags").notNull().default({}),
    createdAt: timestamp("created_at", { withTimezone: true })
      .notNull()
      .defaultNow(),
    updatedAt: timestamp("updated_at", { withTimezone: true })
      .notNull()
      .defaultNow(),
  },
);

export const brandFeatures = pgTable(
  "brand_features",
  {
    id: uuid("id").primaryKey().defaultRandom(),
    brandId: uuid("brand_id")
      .notNull()
      .references(() => brands.id, { onDelete: "cascade" }),
    featureKey: text("feature_key").notNull(),
    enabled: boolean("enabled").notNull().default(false),
    enabledAt: timestamp("enabled_at", { withTimezone: true }),
    disabledAt: timestamp("disabled_at", { withTimezone: true }),
    createdAt: timestamp("created_at", { withTimezone: true })
      .notNull()
      .defaultNow(),
  },
  (t) => ({
    brandFeatureIdx: uniqueIndex("brand_features_brand_key_idx").on(
      t.brandId,
      t.featureKey,
    ),
  }),
);

export type BrandSettings = typeof brandSettings.$inferSelect;
export type BrandFeature = typeof brandFeatures.$inferSelect;
