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

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

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

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

    typeId: text("type_id")
        .notNull()
        .references(() => helpdesk_channel_types.id),

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

    isActive: boolean("is_active").notNull().default(true),

    config: jsonb("config").notNull(),
    publicConfig: jsonb("public_config").notNull().default({}),

    publicToken: text("public_token").unique(),
    secretToken: text("secret_token"),

    createdBy: uuid("created_by").references(() => authUsers.id),

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

export type HelpdeskChannelInstance =
    typeof helpdesk_channel_instances.$inferSelect
export type NewHelpdeskChannelInstance =
    typeof helpdesk_channel_instances.$inferInsert
