import { pgTable, text, unique, uuid } from "drizzle-orm/pg-core";
import { createInsertSchema, createSelectSchema } from "drizzle-zod";
import type z from "zod";
import { user } from "./auth.schema.js";
import { notifications } from "./notifications.schema.js";

export const notificationSub = pgTable(
	"notification_sub",
	{
		id: uuid("id").defaultRandom().primaryKey(),
		userId: text("user_id")
			.notNull()
			.references(() => user.id, { onDelete: "cascade" }),
		notificationId: uuid("notification_id")
			.notNull()
			.references(() => notifications.id, { onDelete: "cascade" }),
		emails: text("emails").array().default([]),
	},
	(table) => ({
		userNotificationUnique: unique(
			"notification_sub_user_notification_unique",
		).on(table.userId, table.notificationId),
	}),
);

export const notificationSubSchema = createSelectSchema(notificationSub);
export const newNotificationSubSchema = createInsertSchema(notificationSub);

export type NotificationSub = z.infer<typeof notificationSubSchema>;
export type NewNotificationSub = z.infer<typeof newNotificationSubSchema>;
