"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.defineQueryFeeModels = exports.AllocationSummary = exports.Transfer = exports.TransferStatus = exports.TransferReceipt = exports.Voucher = exports.AllocationReceipt = void 0;
const sequelize_1 = require("sequelize");
class AllocationReceipt extends sequelize_1.Model {
}
exports.AllocationReceipt = AllocationReceipt;
class Voucher extends sequelize_1.Model {
}
exports.Voucher = Voucher;
class TransferReceipt extends sequelize_1.Model {
}
exports.TransferReceipt = TransferReceipt;
var TransferStatus;
(function (TransferStatus) {
    TransferStatus["OPEN"] = "OPEN";
    TransferStatus["ALLOCATION_CLOSED"] = "ALLOCATION_CLOSED";
    TransferStatus["RESOLVED"] = "RESOLVED";
    TransferStatus["FAILED"] = "FAILED";
})(TransferStatus = exports.TransferStatus || (exports.TransferStatus = {}));
class Transfer extends sequelize_1.Model {
}
exports.Transfer = Transfer;
class AllocationSummary extends sequelize_1.Model {
}
exports.AllocationSummary = AllocationSummary;
function defineQueryFeeModels(sequelize) {
    AllocationReceipt.init({
        // TODO: To distinguish between (id, allocation) pairs from different
        // clients, the primary key should really be (id, allocation,
        // clientAddress)
        id: {
            type: sequelize_1.DataTypes.STRING(66),
            allowNull: false,
            primaryKey: true,
        },
        allocation: {
            type: sequelize_1.DataTypes.STRING(42),
            allowNull: false,
            primaryKey: true,
        },
        signature: {
            type: sequelize_1.DataTypes.STRING(132),
            allowNull: false,
        },
        fees: {
            type: sequelize_1.DataTypes.DECIMAL,
            allowNull: false,
            validate: {
                min: 0.0,
            },
        },
    }, { sequelize, tableName: 'allocation_receipts' });
    Voucher.init({
        allocation: {
            type: sequelize_1.DataTypes.STRING(42),
            allowNull: false,
            primaryKey: true,
        },
        amount: {
            type: sequelize_1.DataTypes.DECIMAL,
            allowNull: false,
            validate: {
                min: 0.0,
            },
        },
        signature: {
            type: sequelize_1.DataTypes.STRING,
            allowNull: false,
        },
    }, { sequelize, tableName: 'vouchers' });
    TransferReceipt.init({
        id: {
            type: sequelize_1.DataTypes.INTEGER,
            allowNull: false,
            primaryKey: true,
        },
        signer: {
            type: sequelize_1.DataTypes.STRING(42),
            allowNull: false,
            primaryKey: true,
        },
        signature: {
            type: sequelize_1.DataTypes.STRING(132),
            allowNull: false,
        },
        fees: {
            type: sequelize_1.DataTypes.DECIMAL,
            allowNull: false,
            validate: {
                min: 0.0,
            },
        },
    }, { sequelize, tableName: 'transfer_receipts' });
    Transfer.init({
        signer: {
            type: sequelize_1.DataTypes.STRING(42),
            allowNull: false,
            primaryKey: true,
        },
        allocation: {
            type: sequelize_1.DataTypes.STRING,
            allowNull: false,
        },
        routingId: {
            type: sequelize_1.DataTypes.STRING(66),
            allowNull: false,
            primaryKey: true,
        },
        allocationClosedAt: {
            type: sequelize_1.DataTypes.DATE,
            allowNull: true,
        },
        status: {
            type: sequelize_1.DataTypes.ENUM(TransferStatus.OPEN, TransferStatus.ALLOCATION_CLOSED, TransferStatus.RESOLVED, TransferStatus.FAILED),
            allowNull: false,
        },
    }, { sequelize, tableName: 'transfers' });
    AllocationSummary.init({
        allocation: {
            type: sequelize_1.DataTypes.STRING(42),
            allowNull: false,
            primaryKey: true,
        },
        closedAt: {
            type: sequelize_1.DataTypes.DATE,
            allowNull: true,
        },
        createdTransfers: {
            type: sequelize_1.DataTypes.INTEGER,
            allowNull: false,
        },
        resolvedTransfers: {
            type: sequelize_1.DataTypes.INTEGER,
            allowNull: false,
        },
        failedTransfers: {
            type: sequelize_1.DataTypes.INTEGER,
            allowNull: false,
        },
        openTransfers: {
            type: sequelize_1.DataTypes.INTEGER,
            allowNull: false,
        },
        collectedFees: {
            type: sequelize_1.DataTypes.DECIMAL,
            allowNull: false,
        },
        withdrawnFees: {
            type: sequelize_1.DataTypes.DECIMAL,
            allowNull: false,
        },
    }, { sequelize, tableName: 'allocation_summaries' });
    Transfer.hasMany(TransferReceipt, {
        sourceKey: 'signer',
        foreignKey: 'signer',
        as: 'receipts',
    });
    TransferReceipt.belongsTo(Transfer, {
        targetKey: 'signer',
        foreignKey: 'signer',
        as: 'transfer',
    });
    AllocationSummary.hasMany(Transfer, {
        sourceKey: 'allocation',
        foreignKey: 'allocation',
        as: 'transfers',
    });
    AllocationSummary.hasMany(AllocationReceipt, {
        sourceKey: 'allocation',
        foreignKey: 'allocation',
        as: 'allocationReceipts',
    });
    AllocationSummary.hasOne(Voucher, {
        sourceKey: 'allocation',
        foreignKey: 'allocation',
        as: 'voucher',
    });
    Transfer.belongsTo(AllocationSummary, {
        targetKey: 'allocation',
        foreignKey: 'allocation',
        as: 'allocationSummary',
    });
    AllocationReceipt.belongsTo(AllocationSummary, {
        targetKey: 'allocation',
        foreignKey: 'allocation',
        as: 'allocationSummary',
    });
    Voucher.belongsTo(AllocationSummary, {
        targetKey: 'allocation',
        foreignKey: 'allocation',
        as: 'allocationSummary',
    });
    return {
        allocationReceipts: AllocationReceipt,
        vouchers: Voucher,
        transferReceipts: TransferReceipt,
        transfers: Transfer,
        allocationSummaries: AllocationSummary,
    };
}
exports.defineQueryFeeModels = defineQueryFeeModels;
//# sourceMappingURL=models.js.map