

module.exports = function(sequelize, DataTypes) {
  const escrow_transactions = sequelize.define(
    'escrow_transactions',
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },
      total_amount: {
        type: DataTypes.DECIMAL,
      },
      client_fee: {
        type: DataTypes.DECIMAL,
      },
      freelancer_fee: {
        type: DataTypes.DECIMAL,
      },
      status: {
        type: DataTypes.ENUM,
        values: [
          "Held_in_Escrow",
          "Released_to_Freelancer",
          "Refunded"
        ],
      },
      importHash: {
        type: DataTypes.STRING(255),
        allowNull: true,
        unique: true,
      },
    },
    {
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
    },
  );

  escrow_transactions.associate = (db) => {
    db.escrow_transactions.belongsTo(db.jobs, {
      as: 'job',
      foreignKey: {
        name: 'jobId',
      },
      constraints: false,
    });

    db.escrow_transactions.belongsTo(db.users, {
      as: 'client',
      foreignKey: {
        name: 'clientId',
      },
      constraints: false,
    });

    db.escrow_transactions.belongsTo(db.users, {
      as: 'freelancer',
      foreignKey: {
        name: 'freelancerId',
      },
      constraints: false,
    });

    db.escrow_transactions.belongsTo(db.users, {
      as: 'createdBy',
    });

    db.escrow_transactions.belongsTo(db.users, {
      as: 'updatedBy',
    });
  };

  return escrow_transactions;
};