const { DataTypes } = require('sequelize'); const { sequelize } = require('../config/database'); const Document = sequelize.define('Document', { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true, }, openaiFileId: { type: DataTypes.STRING, allowNull: true, unique: true, comment: 'OpenAI file ID from their API' }, name: { type: DataTypes.STRING, allowNull: false, }, originalName: { type: DataTypes.STRING, allowNull: false, comment: 'Original filename when uploaded' }, filePath: { type: DataTypes.TEXT, allowNull: true, comment: 'Local file path if stored locally' }, fileSize: { type: DataTypes.BIGINT, allowNull: false, }, mimeType: { type: DataTypes.STRING(100), allowNull: false, }, purpose: { type: DataTypes.ENUM('file-search', 'code-interpreter', 'assistants'), defaultValue: 'file-search', allowNull: false, }, status: { type: DataTypes.ENUM('uploading', 'processed', 'error'), defaultValue: 'uploading', allowNull: false, }, vectorStoreId: { type: DataTypes.UUID, allowNull: true, references: { model: 'vector_stores', key: 'id', }, onUpdate: 'CASCADE', onDelete: 'SET NULL', }, assistantId: { type: DataTypes.UUID, allowNull: true, references: { model: 'assistants', key: 'id', }, onUpdate: 'CASCADE', onDelete: 'CASCADE', }, uploadedByUserId: { type: DataTypes.UUID, allowNull: true, comment: 'User who uploaded the document' }, metadata: { type: DataTypes.JSONB, defaultValue: {}, }, }, { tableName: 'documents', timestamps: true, indexes: [ { fields: ['openaiFileId'], unique: true }, { fields: ['vectorStoreId'] }, { fields: ['assistantId'] }, { fields: ['uploadedByUserId'] }, { fields: ['status'] }, { fields: ['purpose'] }, { fields: ['createdAt'] }, ], }); module.exports = Document;