import { pgTable, text, timestamp, uniqueIndex, index, uuid, boolean } from "drizzle-orm/pg-core";
import { users } from "./auth";
import { familyMemberRole, subscriptionTier, subscriptionStatus } from "./enums";

export const familyGroups = pgTable("family_groups", {
  id: uuid("id").defaultRandom().primaryKey(),
  name: text("name").notNull(),
  ownerId: uuid("owner_id").notNull().references(() => users.id),
  createdAt: timestamp("created_at", { withTimezone: true, mode: "date" }).defaultNow().notNull(),
  updatedAt: timestamp("updated_at", { withTimezone: true, mode: "date" }).defaultNow().notNull().$onUpdate(() => new Date()),
}, (table) => ({
  ownerIdIdx: index("family_groups_owner_id_idx").on(table.ownerId),
  nameIdx: index("family_groups_name_idx").on(table.name),
}));

export const familyGroupMembers = pgTable("family_group_members", {
  id: uuid("id").defaultRandom().primaryKey(),
  groupId: uuid("group_id").notNull().references(() => familyGroups.id, { onDelete: "cascade" }),
  userId: uuid("user_id").notNull().references(() => users.id, { onDelete: "cascade" }),
  role: familyMemberRole("role").default("member").notNull(),
  joinedAt: timestamp("joined_at", { withTimezone: true, mode: "date" }).defaultNow().notNull(),
  createdAt: timestamp("created_at", { withTimezone: true, mode: "date" }).defaultNow().notNull(),
  updatedAt: timestamp("updated_at", { withTimezone: true, mode: "date" }).defaultNow().notNull().$onUpdate(() => new Date()),
}, (table) => ({
  groupUserUnique: uniqueIndex("family_group_members_group_user_unique").on(table.groupId, table.userId),
  groupIdIdx: index("family_group_members_group_id_idx").on(table.groupId),
  userIdIdx: index("family_group_members_user_id_idx").on(table.userId),
}));

export const subscriptions = pgTable("subscriptions", {
  id: uuid("id").defaultRandom().primaryKey(),
  userId: uuid("user_id").notNull().references(() => users.id, { onDelete: "cascade" }),
  familyGroupId: uuid("family_group_id").references(() => familyGroups.id),
  stripeId: text("stripe_id").unique(),
  tier: subscriptionTier("tier").default("basic").notNull(),
  status: subscriptionStatus("status").default("active").notNull(),
  currentPeriodStart: timestamp("current_period_start", { withTimezone: true, mode: "date" }).notNull(),
  currentPeriodEnd: timestamp("current_period_end", { withTimezone: true, mode: "date" }).notNull(),
  cancelAtPeriodEnd: boolean("cancel_at_period_end").default(false).notNull(),
  createdAt: timestamp("created_at", { withTimezone: true, mode: "date" }).defaultNow().notNull(),
  updatedAt: timestamp("updated_at", { withTimezone: true, mode: "date" }).defaultNow().notNull().$onUpdate(() => new Date()),
}, (table) => ({
  userIdIdx: index("subscriptions_user_id_idx").on(table.userId),
  familyGroupIdIdx: index("subscriptions_family_group_id_idx").on(table.familyGroupId),
  stripeIdIdx: index("subscriptions_stripe_id_idx").on(table.stripeId),
  tierIdx: index("subscriptions_tier_idx").on(table.tier),
}));
