import { DataTypes, Model, Optional } from 'sequelize';
import { sequelize } from '@config/database';

interface WorkNoteAttributes {
  noteId: string;
  requestId: string;
  userId: string;
  userName?: string | null;
  userRole?: string | null;
  message: string; // rich text (HTML/JSON) stored as TEXT
  messageType?: string | null; // COMMENT etc
  isPriority?: boolean | null;
  hasAttachment?: boolean | null;
  parentNoteId?: string | null;
  mentionedUsers?: string[] | null;
  reactions?: object | null;
  isEdited?: boolean | null;
  isDeleted?: boolean | null;
  createdAt: Date;
  updatedAt: Date;
}

interface WorkNoteCreationAttributes extends Optional<WorkNoteAttributes, 'noteId' | 'userName' | 'userRole' | 'messageType' | 'isPriority' | 'hasAttachment' | 'parentNoteId' | 'mentionedUsers' | 'reactions' | 'isEdited' | 'isDeleted' | 'createdAt' | 'updatedAt'> {}

class WorkNote extends Model<WorkNoteAttributes, WorkNoteCreationAttributes> implements WorkNoteAttributes {
  public noteId!: string;
  public requestId!: string;
  public userId!: string;
  public userName!: string | null;
  public userRole!: string | null;
  public message!: string;
  public messageType!: string | null;
  public isPriority!: boolean | null;
  public hasAttachment!: boolean | null;
  public parentNoteId!: string | null;
  public mentionedUsers!: string[] | null;
  public reactions!: object | null;
  public isEdited!: boolean | null;
  public isDeleted!: boolean | null;
  public createdAt!: Date;
  public updatedAt!: Date;
}

WorkNote.init(
  {
    noteId: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true, field: 'note_id' },
    requestId: { type: DataTypes.UUID, allowNull: false, field: 'request_id' },
    userId: { type: DataTypes.UUID, allowNull: false, field: 'user_id' },
    userName: { type: DataTypes.STRING(255), allowNull: true, field: 'user_name' },
    userRole: { type: DataTypes.STRING(50), allowNull: true, field: 'user_role' },
    message: { type: DataTypes.TEXT, allowNull: false },
    messageType: { type: DataTypes.STRING(50), allowNull: true, field: 'message_type' },
    isPriority: { type: DataTypes.BOOLEAN, allowNull: true, field: 'is_priority' },
    hasAttachment: { type: DataTypes.BOOLEAN, allowNull: true, field: 'has_attachment' },
    parentNoteId: { type: DataTypes.UUID, allowNull: true, field: 'parent_note_id' },
    mentionedUsers: { type: DataTypes.ARRAY(DataTypes.UUID), allowNull: true, field: 'mentioned_users' },
    reactions: { type: DataTypes.JSONB, allowNull: true },
    isEdited: { type: DataTypes.BOOLEAN, allowNull: true, field: 'is_edited' },
    isDeleted: { type: DataTypes.BOOLEAN, allowNull: true, field: 'is_deleted' },
    createdAt: { type: DataTypes.DATE, allowNull: false, defaultValue: DataTypes.NOW, field: 'created_at' },
    updatedAt: { type: DataTypes.DATE, allowNull: false, defaultValue: DataTypes.NOW, field: 'updated_at' },
  },
  {
    sequelize,
    modelName: 'WorkNote',
    tableName: 'work_notes',
    timestamps: false,
    indexes: [ { fields: ['request_id'] }, { fields: ['user_id'] }, { fields: ['created_at'] } ]
  }
);

export { WorkNote };


