import {
	integer,
	pgEnum,
	pgTable,
	serial,
	text,
	timestamp,
	uuid,
} from "drizzle-orm/pg-core";
import { createInsertSchema, createSelectSchema } from "drizzle-zod";
import { z } from "zod";
import { leases } from "./forkliftLeases.js";
import { serverData } from "./servers.js";

const status = pgEnum("forklift_status", [
	"active",
	"inactive",
	"sold",
	"retired",
	"transferred",
]);

export const forklifts = pgTable("forklifts", {
	forklift_id: uuid("forklift_id").defaultRandom().primaryKey(),
	forkliftNumber: serial("forklift_number").notNull(),
	serialNumber: text("serial_number").unique().notNull(),
	model: text("model").notNull(),
	plant: text("plant")
		.notNull()
		.references(() => serverData.name, { onDelete: "set null" }), // what plant the forklift is in.
	forkliftStatus: text("forklift_status").default("active"), //["active","inactive","sold","retired","transferred",]
	glCode: integer("gl_code").notNull(), // this will be updated on the onconflift update so we dont do anything funny.
	profitCenter: integer("profit_center").notNull(), // should be updated if its changing profit centers per request by the plant
	manufacturer: text("manufacturer").notNull(),
	manufacturerYear: text("manufacturer_year").notNull(),
	engine: text("engine").notNull(),
	batteryType: text("battery_type").notNull(),
	leaseId: uuid("lease_id").references(() => leases.id, {
		onDelete: "set null",
	}),
	dataPlate: text("data_plate"),
	add_date: timestamp("add_date").defaultNow(),
	add_user: text("add_user").default("LST"),
	upd_date: timestamp("upd_date").defaultNow(),
	upd_user: text("upd_user").default("LST"),
});

export const forkliftsSchema = createSelectSchema(forklifts);
export const newForkliftsSchema = createInsertSchema(forklifts);

export type Forklift = z.infer<typeof forkliftsSchema>;
export type NewNewForklift = z.infer<typeof newForkliftsSchema>;
