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

import { tenants } from "./tenants"
import { vendors } from "./vendors"
import { spaces } from "./spaces"
import { authUsers } from "./auth_users"

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

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

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

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

    description: text("description"),

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

    currentSpace: bigint("currentSpace", { mode: "number" }).references(
        () => spaces.id
    ),

    articleNumber: text("articleNumber"),
    serialNumber: text("serialNumber"),

    purchaseDate: date("purchaseDate"),

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

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

    purchasePrice: doublePrecision("purchasePrice").default(0),

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

    currentValue: doublePrecision("currentValue"),

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

    profiles: jsonb("profiles").notNull().default([]),

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

export type InventoryItem = typeof inventoryitems.$inferSelect
export type NewInventoryItem = typeof inventoryitems.$inferInsert
