import { Model, DataTypes, Sequelize } from 'sequelize';

export interface DocumentVersionAttributes {
    id: string;
    documentId: string;
    documentType: string;
    versionNumber: number;
    filePath: string;
    uploadedBy: string | null;
}

export interface DocumentVersionInstance extends Model<DocumentVersionAttributes>, DocumentVersionAttributes { }

export default (sequelize: Sequelize) => {
    const DocumentVersion = sequelize.define<DocumentVersionInstance>('DocumentVersion', {
        id: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            primaryKey: true
        },
        documentId: {
            type: DataTypes.UUID,
            allowNull: false
        },
        documentType: {
            type: DataTypes.STRING,
            allowNull: false
        },
        versionNumber: {
            type: DataTypes.INTEGER,
            allowNull: false
        },
        filePath: {
            type: DataTypes.STRING,
            allowNull: false
        },
        uploadedBy: {
            type: DataTypes.UUID,
            allowNull: true,
            references: {
                model: 'users',
                key: 'id'
            }
        }
    }, {
        tableName: 'document_versions',
        timestamps: true,
        updatedAt: false
    });

    (DocumentVersion as any).associate = (models: any) => {
        DocumentVersion.belongsTo(models.User, { foreignKey: 'uploadedBy', as: 'uploader' });
    };

    return DocumentVersion;
};
