import { randomUUID } from "node:crypto";
import { index, integer, sqliteTable, text } from "drizzle-orm/sqlite-core";
const uuid = (name) => text(name).$defaultFn(() => randomUUID());
const nowFn = () => new Date();
/* ------------------------------------------------------------------ */
/* Identité (cœur — rien ici ne connaît « PrivateFamily »)             */
/* ------------------------------------------------------------------ */
export const users = sqliteTable("users", {
    id: uuid("id").primaryKey(),
    email: text("email").unique(),
    passwordHash: text("password_hash"),
    displayName: text("display_name").notNull(),
    createdAt: integer("created_at", { mode: "timestamp_ms" })
        .notNull()
        .$defaultFn(nowFn),
}, (t) => [index("users_email_idx").on(t.email)]);
export const devices = sqliteTable("devices", {
    id: uuid("id").primaryKey(),
    userId: text("user_id")
        .notNull()
        .references(() => users.id, { onDelete: "cascade" }),
    expoPushToken: text("expo_push_token"),
    platform: text("platform"),
    /** BCP 47 du device (ex. "fr", "pt-BR") — push localisés ; null → fr. */
    locale: text("locale"),
    createdAt: integer("created_at", { mode: "timestamp_ms" })
        .notNull()
        .$defaultFn(nowFn),
    lastSeenAt: integer("last_seen_at", { mode: "timestamp_ms" }),
}, (t) => [index("devices_user_idx").on(t.userId)]);
export const refreshTokens = sqliteTable("refresh_tokens", {
    id: uuid("id").primaryKey(),
    userId: text("user_id")
        .notNull()
        .references(() => users.id, { onDelete: "cascade" }),
    tokenHash: text("token_hash").notNull().unique(),
    deviceId: text("device_id"),
    /**
     * Référence souple vers une famille du module PrivateFamily — pas de FK :
     * le cœur ne connaît pas les tables des modules (dépendance unidirectionnelle).
     */
    familyId: text("family_id"),
    expiresAt: integer("expires_at", { mode: "timestamp_ms" }).notNull(),
    revokedAt: integer("revoked_at", { mode: "timestamp_ms" }),
    createdAt: integer("created_at", { mode: "timestamp_ms" })
        .notNull()
        .$defaultFn(nowFn),
}, (t) => [index("refresh_tokens_user_idx").on(t.userId)]);
/**
 * Clés de maintenance temporaires (accès support). Seul le hash du jeton est
 * stocké — la clé n'est montrée qu'une fois à sa génération. Révocables :
 * `revoked_at` est contrôlé à chaque utilisation.
 */
export const maintenanceKeys = sqliteTable("maintenance_keys", {
    id: uuid("id").primaryKey(),
    tokenHash: text("token_hash").notNull().unique(),
    /** Libellé libre (ex. "support août 2026") pour le suivi côté admin. */
    label: text("label"),
    createdBy: text("created_by")
        .notNull()
        .references(() => users.id),
    expiresAt: integer("expires_at", { mode: "timestamp_ms" }).notNull(),
    revokedAt: integer("revoked_at", { mode: "timestamp_ms" }),
    createdAt: integer("created_at", { mode: "timestamp_ms" })
        .notNull()
        .$defaultFn(nowFn),
}, (t) => [index("maintenance_keys_created_idx").on(t.createdBy)]);
//# sourceMappingURL=schema.js.map