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

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

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

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

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

    state: text("state").notNull().default("Entwurf"),

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

    reference: text("reference"),
    date: text("date"),

    document: bigint("document", { mode: "number" }),

    dueDate: text("dueDate"),

    description: text("description"),

    paymentType: text("paymentType"),

    preparationSource: text("preparation_source"),
    eInvoiceSyntax: text("e_invoice_syntax"),
    eInvoiceProfile: text("e_invoice_profile"),
    eInvoiceValidation: jsonb("e_invoice_validation"),

    accounts: jsonb("accounts").notNull().default([
        {
            account: null,
            taxType: null,
            amountNet: null,
            amountTax: 19,
            costCentre: null,
        },
    ]),

    paid: boolean("paid").notNull().default(false),
    expense: boolean("expense").notNull().default(true),

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

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

export type IncomingInvoice = typeof incominginvoices.$inferSelect
export type NewIncomingInvoice = typeof incominginvoices.$inferInsert
