import {
	boolean,
	integer,
	pgTable,
	text,
	timestamp,
	uniqueIndex,
	uuid,
} from "drizzle-orm/pg-core";
import { createInsertSchema, createSelectSchema } from "drizzle-zod";
import z from "zod";

export const serverData = pgTable(
	"serverData",
	{
		server_id: uuid("server_id").defaultRandom().primaryKey(),
		name: text("name").notNull().unique(),
		serverDNS: text("serverDNS").notNull(),
		plantToken: text("plantToken").notNull(),
		ipAddress: text("ipAddress").notNull(),
		greatPlainsPlantCode: integer("greatPlainsPlantCode").notNull(),
		streetAddress: text("streetAddress"),
		cityState: text("cityState"),
		zipcode: integer("zipcode"),
		contactEmail: text("contactEmail"),
		contactPhone: text("contactPhone"),
		customerTiAcc: text("customerTiAcc"),
		lstServerPort: integer("lstServerPort").notNull(),
		active: boolean("active").default(true),
		serverLoc: text("serverLoc").notNull(),
		lastUpdated: timestamp("lastUpdated").defaultNow(),
		isUpgrading: boolean("isUpgrading").default(false),
		add_user: text("add_user").default("lst_user"),
		add_date: timestamp("add_date").defaultNow(),
		upd_user: text("upd_user").default("lst_user"),
		upd_date: timestamp("upd_date").defaultNow(),
	},

	(table) => [
		// uniqueIndex('emailUniqueIndex').on(sql`lower(${table.email})`),
		uniqueIndex("plantToken").on(table.plantToken),
	],
);

export const selectServerDataSchema = createSelectSchema(serverData);

export const insertServerDataSchema = createInsertSchema(serverData).extend({
	contactEmail: z.email().optional(),
	// zipcode: z
	//     .string()
	//     .regex(/^\d{5}$/)
	//     .optional(),
});
