const config = require('../../config');
const providers = config.providers;
const crypto = require('crypto');
const bcrypt = require('bcrypt');
const moment = require('moment');

module.exports = function(sequelize, DataTypes) {
  const contracts = sequelize.define(
    'contracts',
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },

contract_number: {
        type: DataTypes.TEXT,
      
      

      },

title: {
        type: DataTypes.TEXT,
      
      

      },

contract_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"works",


"goods",


"services",


"consulting",


"grant_agreement"

        ],

      },

contract_value: {
        type: DataTypes.DECIMAL,
      
      

      },

currency: {
        type: DataTypes.ENUM,
      
      

        values: [

"CDF",


"USD"

        ],

      },

start_date: {
        type: DataTypes.DATE,
      
      

      },

end_date: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"under_review",


"active",


"suspended",


"completed",


"terminated",


"expired",


"closed"

        ],

      },

retention_percent: {
        type: DataTypes.DECIMAL,
      
      

      },

penalty_rate_percent: {
        type: DataTypes.DECIMAL,
      
      

      },

signed_at: {
        type: DataTypes.DATE,
      
      

      },

      importHash: {
        type: DataTypes.STRING(255),
        allowNull: true,
        unique: true,
      },
    },
    {
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
    },
  );

  contracts.associate = (db) => {


/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity



































    db.contracts.hasMany(db.contract_amendments, {
      as: 'contract_amendments_contract',
      foreignKey: {
          name: 'contractId',
      },
      constraints: false,
    });


    db.contracts.hasMany(db.contract_milestones, {
      as: 'contract_milestones_contract',
      foreignKey: {
          name: 'contractId',
      },
      constraints: false,
    });








    db.contracts.hasMany(db.invoices, {
      as: 'invoices_contract',
      foreignKey: {
          name: 'contractId',
      },
      constraints: false,
    });





    db.contracts.hasMany(db.obligations, {
      as: 'obligations_contract',
      foreignKey: {
          name: 'contractId',
      },
      constraints: false,
    });






//end loop



    db.contracts.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });

    db.contracts.belongsTo(db.vendors, {
      as: 'vendor',
      foreignKey: {
        name: 'vendorId',
      },
      constraints: false,
    });

    db.contracts.belongsTo(db.awards, {
      as: 'award',
      foreignKey: {
        name: 'awardId',
      },
      constraints: false,
    });

    db.contracts.belongsTo(db.projects, {
      as: 'project',
      foreignKey: {
        name: 'projectId',
      },
      constraints: false,
    });

    db.contracts.belongsTo(db.budget_lines, {
      as: 'budget_line',
      foreignKey: {
        name: 'budget_lineId',
      },
      constraints: false,
    });




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

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



  return contracts;
};


