import { pgTable, serial, integer, timestamp, index } from "drizzle-orm/pg-core";
import { usersTable } from "./users";
import { appsTable } from "./apps";

export const appOpensTable = pgTable(
  "app_opens",
  {
    id: serial("id").primaryKey(),
    userId: integer("user_id")
      .notNull()
      .references(() => usersTable.id, { onDelete: "cascade" }),
    appId: integer("app_id")
      .notNull()
      .references(() => appsTable.id, { onDelete: "cascade" }),
    createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
  },
  (t) => [
    index("app_opens_created_at_idx").on(t.createdAt),
    index("app_opens_app_id_idx").on(t.appId),
  ],
);

export type AppOpen = typeof appOpensTable.$inferSelect;
