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,
      },

name: {
        type: DataTypes.TEXT,
      
      

      },

contract_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"maintenance",


"msa",


"sow",


"nda",


"hosting",


"other"

        ],

      },

billing_model: {
        type: DataTypes.ENUM,
      
      

        values: [

"fixed_price",


"time_material",


"recurring"

        ],

      },

amount: {
        type: DataTypes.DECIMAL,
      
      

      },

currency: {
        type: DataTypes.ENUM,
      
      

        values: [

"USD",


"CDF",


"EUR",


"XAF",


"XOF"

        ],

      },

start_at: {
        type: DataTypes.DATE,
      
      

      },

end_at: {
        type: DataTypes.DATE,
      
      

      },

auto_renew: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

renewal_notice_days: {
        type: DataTypes.INTEGER,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"active",


"suspended",


"expired",


"terminated"

        ],

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

      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.invoices, {
      as: 'invoices_contract',
      foreignKey: {
          name: 'contractId',
      },
      constraints: false,
    });





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









//end loop



    db.contracts.belongsTo(db.tenants, {
      as: 'tenant',
      foreignKey: {
        name: 'tenantId',
      },
      constraints: false,
    });

    db.contracts.belongsTo(db.companies, {
      as: 'company',
      foreignKey: {
        name: 'companyId',
      },
      constraints: false,
    });

    db.contracts.belongsTo(db.service_lines, {
      as: 'service_line',
      foreignKey: {
        name: 'service_lineId',
      },
      constraints: false,
    });

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




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

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



  return contracts;
};


