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

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

export interface LoaApprovalInstance extends Model<LoaApprovalAttributes>, LoaApprovalAttributes { }

export default (sequelize: Sequelize) => {
    const LoaApproval = sequelize.define<LoaApprovalInstance>('LoaApproval', {
        id: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            primaryKey: true
        },
        requestId: {
            type: DataTypes.UUID,
            allowNull: false,
            references: {
                model: 'loa_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: 'loa_approvals',
        timestamps: true,
        updatedAt: false
    });

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

    return LoaApproval;
};
