import { boolean, integer, pgEnum, pgTable, text, uuid } from "drizzle-orm/pg-core";
import { MEASUREMENT_SYSTEMS, TEMPERATURE_UNITS, tuple } from "@app/shared-types";
import { updatedAt } from "./_shared.js";
import { users } from "./users.js";

export const measurementSystemEnum = pgEnum("measurement_system", tuple(MEASUREMENT_SYSTEMS));
export const temperatureUnitEnum = pgEnum("temperature_unit", tuple(TEMPERATURE_UNITS));

/**
 * Locale-preferenser per användare (i18n-spec §6, §29):
 * språk, region, tidszon, måttsystem, temperatur, valuta, veckostart, 12/24 h –
 * alla oberoende av varandra. Saknas rad gäller regiondefaults (SE).
 */
export const userLocalePreferences = pgTable("user_locale_preferences", {
  userId: uuid("user_id")
    .primaryKey()
    .references(() => users.id, { onDelete: "cascade" }),
  /** BCP 47, t.ex. sv-SE, en-US. */
  languageTag: text("language_tag").notNull().default("sv-SE"),
  /** ISO 3166-1 alpha-2. */
  regionCode: text("region_code").notNull().default("SE"),
  /** IANA-tidszon. */
  timeZone: text("time_zone").notNull().default("Europe/Stockholm"),
  measurementSystem: measurementSystemEnum("measurement_system").notNull().default("METRIC"),
  temperatureUnit: temperatureUnitEnum("temperature_unit").notNull().default("CELSIUS"),
  /** ISO 4217. */
  currencyCode: text("currency_code").notNull().default("SEK"),
  /** 0 = söndag … 6 = lördag. */
  firstDayOfWeek: integer("first_day_of_week").notNull().default(1),
  use24HourTime: boolean("use_24_hour_time").notNull().default(true),
  updatedAt: updatedAt(),
});
