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

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

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

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

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

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

    notes: text("notes"),

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

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

    description: json("description"),

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

    heroId: text("heroId"),

    measure: text("measure"),

    material: jsonb("material"),

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

    profiles: uuid("profiles").array().notNull().default([]),

    projectNumber: text("projectNumber"),

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

    projectType: text("projectType").default("Projekt"),

    projecttype: bigint("projecttype", { mode: "number" }).references(
        () => projecttypes.id
    ),

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

    customerRef: text("customerRef"),

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

    active_phase: text("active_phase").default("Erstkontakt"),
})

export type Project = typeof projects.$inferSelect
export type NewProject = typeof projects.$inferInsert
