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

import { tenants } from "./tenants"
import { projects } from "./projects"
import { customers } from "./customers"
import { contracts } from "./contracts"
import { vendors } from "./vendors"
import { incominginvoices } from "./incominginvoices"
import { plants } from "./plants"
import { createddocuments } from "./createddocuments"
import { vehicles } from "./vehicles"
import { products } from "./products"
import { inventoryitems } from "./inventoryitems"
import { folders } from "./folders"
import { filetags } from "./filetags"
import { authUsers } from "./auth_users"
import { authProfiles } from "./auth_profiles"
import { spaces } from "./spaces"
import { documentboxes } from "./documentboxes"
import { checks } from "./checks"

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

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

    path: text("path"),

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

    project: bigint("project", { mode: "number" }).references(() => projects.id),
    customer: bigint("customer", { mode: "number" }).references(() => customers.id),
    contract: bigint("contract", { mode: "number" }).references(() => contracts.id),
    vendor: bigint("vendor", { mode: "number" }).references(() => vendors.id),
    incominginvoice: bigint("incominginvoice", { mode: "number" }).references(() => incominginvoices.id),
    plant: bigint("plant", { mode: "number" }).references(() => plants.id),
    createddocument: bigint("createddocument", { mode: "number" }).references(() => createddocuments.id),
    vehicle: bigint("vehicle", { mode: "number" }).references(() => vehicles.id),
    product: bigint("product", { mode: "number" }).references(() => products.id),

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

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

    folder: uuid("folder").references(() => folders.id),

    mimeType: text("mimeType"),

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

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

    type: uuid("type").references(() => filetags.id),

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

    name: text("name"),
    extractedText: text("extracted_text"),

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

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

    authProfile: uuid("auth_profile").references(() => authProfiles.id),
    size: bigint("size", { mode: "number" }),
})

export type File = typeof files.$inferSelect
export type NewFile = typeof files.$inferInsert
