// src/server/features/notes/db/note_history.table.ts
// Note history table schema for audit log

import { mysqlTable, varchar, timestamp, mysqlEnum, json } from 'drizzle-orm/mysql-core';
import { notes } from './notes.table';
import { employees } from '@features/personnel/db/employees.table';

export const noteHistory = mysqlTable('note_history', {
  id: varchar('id', { length: 36 }).primaryKey(),
  noteId: varchar('note_id', { length: 36 })
    .notNull()
    .references(() => notes.id, { onDelete: 'cascade' }),
  // Action type: created, updated, status_changed, commented, returned, priority_changed
  action: mysqlEnum('action', ['created', 'updated', 'status_changed', 'commented', 'returned', 'priority_changed'])
    .notNull(),
  // Payload: JSON with additional data (e.g., old status, new status, comment text)
  payload: json('payload').$type<{
    oldStatus?: string;
    newStatus?: string;
    commentText?: string;
    reason?: string;
    changes?: Record<string, any>;
  }>(),
  authorId: varchar('author_id', { length: 36 })
    .notNull()
    .references(() => employees.id, { onDelete: 'cascade' }),
  createdAt: timestamp('created_at').notNull().defaultNow(),
});
