import {
	boolean,
	jsonb,
	pgEnum,
	pgTable,
	text,
	timestamp,
	unique,
	uuid,
} from "drizzle-orm/pg-core";
import { createInsertSchema, createSelectSchema } from "drizzle-zod";
import type z from "zod";

export const mobileRoleEnum = pgEnum("mobile_role", [
	"user",
	"lead",
	"manager",
	"admin",
]);

export const scanUser = pgTable(
	"scan_users",
	{
		id: uuid("id").defaultRandom().primaryKey(),
		name: text("name").notNull(), // the user that will be using the scanner
		scannerId: text("scanner_id").unique().notNull(),
		pinNumber: text("pin_number").unique().notNull(),
		pinHash: text("pin_hash").notNull(),
		excludedCommand: jsonb("excluded_commands").default([]),
		role: mobileRoleEnum("role").notNull().default("user"),
		active: boolean("active").default(true),
		lastScan: timestamp("last_scan").defaultNow(),
		add_Date: timestamp("add_Date", { withTimezone: true }).defaultNow(),
		upd_date: timestamp("upd_date", { withTimezone: true }).defaultNow(),
	},
	(table) => ({
		userNotificationUnique: unique("scan_user_unique").on(
			table.scannerId,
			table.pinNumber,
		),
	}),
);

export const scanUserSchema = createSelectSchema(scanUser);
export const newsSanUserSchema = createInsertSchema(scanUser);

export type ScanUser = z.infer<typeof scanUserSchema>;
export type NewScanUser = z.infer<typeof newsSanUserSchema>;
