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

import { tenants } from "./tenants"
import { customers } from "./customers"
import { entitybankaccounts } from "./entitybankaccounts"
import { authUsers } from "./auth_users"

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

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

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

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

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

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

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

    mandateType: text("mandate_type").notNull().default("CORE"),

    sequenceType: text("sequence_type").notNull().default("RCUR"),

    signedAt: timestamp("signed_at", { withTimezone: true }),

    validFrom: timestamp("valid_from", { withTimezone: true }),

    validUntil: timestamp("valid_until", { withTimezone: true }),

    defaultMandate: boolean("default_mandate").notNull().default(false),

    notes: text("notes"),

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

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

export type OutgoingSepaMandate = typeof outgoingsepamandates.$inferSelect
export type NewOutgoingSepaMandate = typeof outgoingsepamandates.$inferInsert
