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

import { tenants } from "./tenants"
import { inventoryitems } from "./inventoryitems"
import { projects } from "./projects"
import { vehicles } from "./vehicles"
import { authUsers } from "./auth_users"
import { branches } from "./branches"

export const costcentres = pgTable("costcentres", {
    id: uuid("id").primaryKey().defaultRandom(),

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

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

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

    parentCostcentre: uuid("parent_costcentre").references(() => costcentres.id),

    vehicle: bigint("vehicle", { mode: "number" }).references(() => vehicles.id),

    project: bigint("project", { mode: "number" }).references(() => projects.id),

    branch: bigint("branch", { mode: "number" }).references(() => branches.id),

    inventoryitem: bigint("inventoryitem", { mode: "number" }).references(
        () => inventoryitems.id
    ),

    description: text("description"),

    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 CostCentre = typeof costcentres.$inferSelect
export type NewCostCentre = typeof costcentres.$inferInsert
