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

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

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

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

        tenant: bigint("tenant", { mode: "number" }).notNull(),

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

        startDate: timestamp("startDate", { withTimezone: true }).notNull(),
        endDate: timestamp("endDate", { withTimezone: true }),

        eventtype: text("eventtype").default("Umsetzung"),
        quick: boolean("quick").notNull().default(false),
        state: text("state").notNull().default("Final"),
        color: text("color"),
        repeatInterval: text("repeatInterval").notNull().default("Keine Wiederholung"),

        project: bigint("project", { mode: "number" }), // FK follows when projects.ts exists

        resources: jsonb("resources").default([]),
        notes: text("notes"),
        link: text("link"),

        profiles: jsonb("profiles").notNull().default([]),
        archived: boolean("archived").notNull().default(false),

        vehicles: jsonb("vehicles").notNull().default([]),
        inventoryitems: jsonb("inventoryitems").notNull().default([]),
        inventoryitemgroups: jsonb("inventoryitemgroups").notNull().default([]),

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

        vendor: bigint("vendor", { mode: "number" }), // will link once vendors.ts is created

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

export type Event = typeof events.$inferSelect
export type NewEvent = typeof events.$inferInsert
