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

export interface ApplicationProgressAttributes {
    id: string;
    applicationId: string;
    stageName: string;
    stageOrder: number;
    status: string;
    completionPercentage: number;
    stageStartedAt: Date | null;
    stageCompletedAt: Date | null;
}

export interface ApplicationProgressInstance extends Model<ApplicationProgressAttributes>, ApplicationProgressAttributes { }

export default (sequelize: Sequelize) => {
    const ApplicationProgress = sequelize.define<ApplicationProgressInstance>('ApplicationProgress', {
        id: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            primaryKey: true
        },
        applicationId: {
            type: DataTypes.UUID,
            allowNull: false,
            references: {
                model: 'applications',
                key: 'id'
            }
        },
        stageName: {
            type: DataTypes.STRING,
            allowNull: false
        },
        stageOrder: {
            type: DataTypes.INTEGER,
            allowNull: false
        },
        status: {
            type: DataTypes.STRING,
            defaultValue: 'pending'
        },
        completionPercentage: {
            type: DataTypes.INTEGER,
            defaultValue: 0
        },
        stageStartedAt: {
            type: DataTypes.DATE,
            allowNull: true
        },
        stageCompletedAt: {
            type: DataTypes.DATE,
            allowNull: true
        }
    }, {
        tableName: 'application_progress',
        timestamps: true
    });

    (ApplicationProgress as any).associate = (models: any) => {
        ApplicationProgress.belongsTo(models.Application, { foreignKey: 'applicationId', as: 'application' });
    };

    return ApplicationProgress;
};
