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

interface WorkNoteAttachmentAttributes {
  attachmentId: string;
  noteId: string;
  fileName: string;
  fileType: string;
  fileSize: number;
  filePath: string;
  storageUrl?: string | null;
  isDownloadable?: boolean | null;
  downloadCount?: number | null;
  uploadedAt: Date;
}

interface WorkNoteAttachmentCreationAttributes extends Optional<WorkNoteAttachmentAttributes, 'attachmentId' | 'storageUrl' | 'isDownloadable' | 'downloadCount' | 'uploadedAt'> {}

class WorkNoteAttachment extends Model<WorkNoteAttachmentAttributes, WorkNoteAttachmentCreationAttributes> implements WorkNoteAttachmentAttributes {
  public attachmentId!: string;
  public noteId!: string;
  public fileName!: string;
  public fileType!: string;
  public fileSize!: number;
  public filePath!: string;
  public storageUrl!: string | null;
  public isDownloadable!: boolean | null;
  public downloadCount!: number | null;
  public uploadedAt!: Date;
}

WorkNoteAttachment.init(
  {
    attachmentId: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true, field: 'attachment_id' },
    noteId: { type: DataTypes.UUID, allowNull: false, field: 'note_id' },
    fileName: { type: DataTypes.STRING(255), allowNull: false, field: 'file_name' },
    fileType: { type: DataTypes.STRING(100), allowNull: false, field: 'file_type' },
    fileSize: { type: DataTypes.BIGINT, allowNull: false, field: 'file_size' },
    filePath: { type: DataTypes.STRING(500), allowNull: false, field: 'file_path' },
    storageUrl: { type: DataTypes.STRING(500), allowNull: true, field: 'storage_url' },
    isDownloadable: { type: DataTypes.BOOLEAN, allowNull: true, field: 'is_downloadable' },
    downloadCount: { type: DataTypes.INTEGER, allowNull: true, field: 'download_count', defaultValue: 0 },
    uploadedAt: { type: DataTypes.DATE, allowNull: false, defaultValue: DataTypes.NOW, field: 'uploaded_at' },
  },
  {
    sequelize,
    modelName: 'WorkNoteAttachment',
    tableName: 'work_note_attachments',
    timestamps: false,
    indexes: [ { fields: ['note_id'] }, { fields: ['uploaded_at'] } ]
  }
);

export { WorkNoteAttachment };


