import {
    pgTable,
    bigint,
    timestamp,
    text,
    boolean,
    doublePrecision,
    uuid,
    date,
} from "drizzle-orm/pg-core"

import { tenants } from "./tenants"
import { customers } from "./customers"
import { customerspaces } from "./customerspaces"
import { products } from "./products"
import { vendors } from "./vendors"
import { authUsers } from "./auth_users"

export const customerinventoryitems = pgTable("customerinventoryitems", {
    id: bigint("id", { mode: "number" })
        .primaryKey()
        .generatedByDefaultAsIdentity(),

    createdAt: timestamp("created_at", { withTimezone: true })
        .notNull()
        .defaultNow(),

    name: text("name").notNull(),

    description: text("description"),

    tenant: bigint("tenant", { mode: "number" })
        .notNull()
        .references(() => tenants.id),

    customer: bigint("customer", { mode: "number" })
        .notNull()
        .references(() => customers.id),

    customerspace: bigint("customerspace", { mode: "number" }).references(
        () => customerspaces.id
    ),

    customerInventoryId: text("customerInventoryId").notNull(),
    serialNumber: text("serialNumber"),

    quantity: bigint("quantity", { mode: "number" }).notNull().default(0),

    manufacturer: text("manufacturer"),
    manufacturerNumber: text("manufacturerNumber"),

    purchaseDate: date("purchaseDate"),
    purchasePrice: doublePrecision("purchasePrice").default(0),
    currentValue: doublePrecision("currentValue"),

    product: bigint("product", { mode: "number" }).references(() => products.id),
    vendor: bigint("vendor", { mode: "number" }).references(() => vendors.id),

    archived: boolean("archived").notNull().default(false),

    updatedAt: timestamp("updated_at", { withTimezone: true }),
    updatedBy: uuid("updated_by").references(() => authUsers.id),
})

export type CustomerInventoryItem = typeof customerinventoryitems.$inferSelect
export type NewCustomerInventoryItem = typeof customerinventoryitems.$inferInsert
