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

export interface LoiApprovalAttributes {
    id: string;
    requestId: string;
    level: number;
    approverRole: string;
    approverId: string | null;
    action: string;
    remarks: string | null;
}

export interface LoiApprovalInstance extends Model<LoiApprovalAttributes>, LoiApprovalAttributes { }

export default (sequelize: Sequelize) => {
    const LoiApproval = sequelize.define<LoiApprovalInstance>('LoiApproval', {
        id: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            primaryKey: true
        },
        requestId: {
            type: DataTypes.UUID,
            allowNull: false,
            references: {
                model: 'loi_requests',
                key: 'id'
            }
        },
        level: {
            type: DataTypes.INTEGER,
            allowNull: false
        },
        approverRole: {
            type: DataTypes.STRING,
            allowNull: false
        },
        approverId: {
            type: DataTypes.UUID,
            allowNull: true,
            references: {
                model: 'users',
                key: 'id'
            }
        },
        action: {
            type: DataTypes.STRING,
            allowNull: false
        },
        remarks: {
            type: DataTypes.TEXT,
            allowNull: true
        }
    }, {
        tableName: 'loi_approvals',
        timestamps: true,
        updatedAt: false
    });

    (LoiApproval as any).associate = (models: any) => {
        LoiApproval.belongsTo(models.LoiRequest, { foreignKey: 'requestId', as: 'request' });
        LoiApproval.belongsTo(models.User, { foreignKey: 'approverId', as: 'approver' });
    };

    return LoiApproval;
};
