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

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

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

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

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

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

    // FIXED: user_id statt profile, verweist auf auth_users.id
    userId: uuid("user_id").references(() => authUsers.id),

    project: bigint("project", { mode: "number" }),
    plant: bigint("plant", { mode: "number" }),

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

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

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

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

export type Task = typeof tasks.$inferSelect
export type NewTask = typeof tasks.$inferInsert
