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

export interface EorChecklistAttributes {
    id: string;
    applicationId: string | null;
    relocationId: string | null;
    auditorId: string | null;
    auditDate: Date | null;
    status: string;
    overallComments: string | null;
}

export interface EorChecklistInstance extends Model<EorChecklistAttributes>, EorChecklistAttributes { }

export default (sequelize: Sequelize) => {
    const EorChecklist = sequelize.define<EorChecklistInstance>('EorChecklist', {
        id: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            primaryKey: true
        },
        applicationId: {
            type: DataTypes.UUID,
            allowNull: true,
            references: {
                model: 'applications',
                key: 'id'
            }
        },
        relocationId: {
            type: DataTypes.UUID,
            allowNull: true,
            references: {
                model: 'relocation_requests',
                key: 'id'
            }
        },
        auditorId: {
            type: DataTypes.UUID,
            allowNull: true,
            references: {
                model: 'users',
                key: 'id'
            }
        },
        auditDate: {
            type: DataTypes.DATE,
            allowNull: true
        },
        status: {
            type: DataTypes.STRING,
            defaultValue: 'pending'
        },
        overallComments: {
            type: DataTypes.TEXT,
            allowNull: true
        }
    }, {
        tableName: 'eor_checklists',
        timestamps: true
    });

    (EorChecklist as any).associate = (models: any) => {
        EorChecklist.belongsTo(models.Application, { foreignKey: 'applicationId', as: 'application' });
        EorChecklist.belongsTo(models.RelocationRequest, { foreignKey: 'relocationId', as: 'relocation' });
        EorChecklist.belongsTo(models.User, { foreignKey: 'auditorId', as: 'auditor' });

        EorChecklist.hasMany(models.EorChecklistItem, { foreignKey: 'checklistId', as: 'items' });
    };

    return EorChecklist;
};
