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

import { tenants } from "./tenants"
import { customers } from "./customers"
import { contacts } from "./contacts"
import { helpdesk_channel_instances } from "./helpdesk_channel_instances" // placeholder

export const helpdesk_contacts = pgTable("helpdesk_contacts", {
    id: uuid("id").primaryKey().defaultRandom(),

    tenantId: bigint("tenant_id", { mode: "number" })
        .notNull()
        .references(() => tenants.id, { onDelete: "cascade" }),

    customerId: bigint("customer_id", { mode: "number" })
        .references(() => customers.id, { onDelete: "set null" }),

    email: text("email"),
    phone: text("phone"),

    externalRef: jsonb("external_ref"),
    displayName: text("display_name"),

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

    sourceChannelId: uuid("source_channel_id").references(
        () => helpdesk_channel_instances.id,
        { onDelete: "set null" }
    ),

    contactId: bigint("contact_id", { mode: "number" }).references(
        () => contacts.id,
        { onDelete: "set null" }
    ),
})

export type HelpdeskContact = typeof helpdesk_contacts.$inferSelect
export type NewHelpdeskContact = typeof helpdesk_contacts.$inferInsert
