import { integer, pgTable, text, timestamp, uuid } from 'drizzle-orm/pg-core'

export const supportMessages = pgTable('support_messages', {
  id: uuid('id').primaryKey().defaultRandom(),
  parentType: text('parent_type').notNull(),
  parentId: text('parent_id').notNull(),
  authorType: text('author_type').notNull(),
  body: text('body').notNull(),
  visibility: text('visibility'),
  createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
})

export const supportStateTransitions = pgTable('support_state_transitions', {
  id: uuid('id').primaryKey().defaultRandom(),
  parentType: text('parent_type').notNull(),
  parentId: text('parent_id').notNull(),
  fromStatus: text('from_status').notNull(),
  toStatus: text('to_status').notNull(),
  actorId: text('actor_id').notNull(),
  reason: text('reason'),
  at: timestamp('at', { withTimezone: true }).notNull().defaultNow(),
})

export const supportAttachments = pgTable('support_attachments', {
  id: uuid('id').primaryKey().defaultRandom(),
  parentType: text('parent_type').notNull(),
  parentId: text('parent_id').notNull(),
  objectKey: text('object_key').notNull(),
  fileName: text('file_name'),
  mimeType: text('mime_type'),
  createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
})

export const slaPolicies = pgTable('sla_policies', {
  scopeKey: text('scope_key').primaryKey(),
  target: text('target').notNull(),
  dueMinutes: integer('due_minutes').notNull(),
})

export const helpdeskSchema = {
  supportMessages,
  supportStateTransitions,
  supportAttachments,
  slaPolicies,
}

export type HelpdeskSchema = typeof helpdeskSchema
