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

import { tenants } from "./tenants"
import { customers } from "./customers"
import { vendors } from "./vendors"
import { projects } from "./projects"
import { plants } from "./plants"
import { incominginvoices } from "./incominginvoices"
import { contacts } from "./contacts"
import { inventoryitems } from "./inventoryitems"
import { products } from "./products"
import { tasks } from "./tasks"
import { vehicles } from "./vehicles"
import { bankstatements } from "./bankstatements"
import { spaces } from "./spaces"
import { customerspaces } from "./customerspaces"
import { customerinventoryitems } from "./customerinventoryitems"
import { costcentres } from "./costcentres"
import { ownaccounts } from "./ownaccounts"
import { createddocuments } from "./createddocuments"
import { documentboxes } from "./documentboxes"
import { hourrates } from "./hourrates"
import { projecttypes } from "./projecttypes"
import { checks } from "./checks"
import { services } from "./services"
import { events } from "./events"
import { inventoryitemgroups } from "./inventoryitemgroups"
import { authUsers } from "./auth_users"
import {files} from "./files";
import { memberrelations } from "./memberrelations";
import { contracts } from "./contracts";
import { outgoingsepamandates } from "./outgoingsepamandates";

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

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

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

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

    contract: bigint("contract", { mode: "number" }).references(
        () => contracts.id,
        { onDelete: "cascade" }
    ),

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

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

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

    plant: bigint("plant", { mode: "number" }).references(
        () => plants.id,
        { onDelete: "cascade" }
    ),

    incomingInvoice: bigint("incomingInvoice", { mode: "number" }).references(
        () => incominginvoices.id,
        { onDelete: "cascade" }
    ),

    contact: bigint("contact", { mode: "number" }).references(() => contacts.id, {
        onDelete: "cascade",
    }),

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

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

    event: bigint("event", { mode: "number" }).references(() => events.id),

    newVal: text("newVal"),
    oldVal: text("oldVal"),

    task: bigint("task", { mode: "number" }).references(() => tasks.id),

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

    bankstatement: bigint("bankstatement", { mode: "number" }).references(
        () => bankstatements.id
    ),

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

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

    customerinventoryitem: bigint("customerinventoryitem", { mode: "number" }).references(() => customerinventoryitems.id),

    memberrelation: bigint("memberrelation", { mode: "number" }).references(() => memberrelations.id),

    outgoingsepamandate: bigint("outgoingsepamandate", { mode: "number" }).references(
        () => outgoingsepamandates.id,
        { onDelete: "cascade" }
    ),

    config: jsonb("config"),

    projecttype: bigint("projecttype", { mode: "number" }).references(
        () => projecttypes.id
    ),

    check: uuid("check").references(() => checks.id),

    service: bigint("service", { mode: "number" }).references(
        () => services.id
    ),

    createddocument: bigint("createddocument", { mode: "number" }).references(
        () => createddocuments.id
    ),

    file: uuid("file").references(() => files.id),

    inventoryitemgroup: uuid("inventoryitemgroup").references(
        () => inventoryitemgroups.id
    ),

    source: text("source").default("Software"),

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

    ownaccount: uuid("ownaccount").references(() => ownaccounts.id),

    documentbox: uuid("documentbox").references(() => documentboxes.id),

    hourrate: uuid("hourrate").references(() => hourrates.id),

    createdBy: uuid("created_by").references(() => authUsers.id),

    action: text("action"),
})

export type HistoryItem = typeof historyitems.$inferSelect
export type NewHistoryItem = typeof historyitems.$inferInsert
