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

import { tenants } from "./tenants"
import { customers } from "./customers"
import { contacts } from "./contacts"
import { contracts } from "./contracts"
import { letterheads } from "./letterheads"
import { projects } from "./projects"
import { plants } from "./plants"
import { authUsers } from "./auth_users"
import {serialExecutions} from "./serialexecutions";
import { outgoingsepamandates } from "./outgoingsepamandates"
import { costcentres } from "./costcentres"

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

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

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

    type: text("type").notNull().default("INVOICE"),

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

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

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

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

    documentNumber: text("documentNumber"),
    documentDate: text("documentDate"),

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

    info: jsonb("info"),

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

    title: text("title"),
    description: text("description"),

    startText: text("startText"),
    endText: text("endText"),

    rows: jsonb("rows").default([]),

    deliveryDateType: text("deliveryDateType"),
    paymentDays: smallint("paymentDays"),
    deliveryDate: text("deliveryDate"),

    contactPerson: uuid("contactPerson"),

    serialConfig: jsonb("serialConfig").default({}),

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

    agriculture: jsonb("agriculture"),

    letterhead: bigint("letterhead", { mode: "number" }).references(
        () => letterheads.id
    ),

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

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

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

    deliveryDateEnd: text("deliveryDateEnd"),

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

    taxType: text("taxType"),

    customSurchargePercentage: doublePrecision("customSurchargePercentage")
        .notNull()
        .default(0),

    report: jsonb("report").notNull().default({}),

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

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

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

    payment_type: text("payment_type").default("transfer"),

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

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

    serialexecution: uuid("serialexecution").references(() => serialExecutions.id)
})

export type CreatedDocument = typeof createddocuments.$inferSelect
export type NewCreatedDocument = typeof createddocuments.$inferInsert
