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 brand_rate_cards = sequelize.define(
    'brand_rate_cards',
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },

      name: {
        type: DataTypes.TEXT,
      },

      rate_card_type: {
        type: DataTypes.TEXT,
      },

      status: {
        type: DataTypes.TEXT,
      },

      currency: {
        type: DataTypes.TEXT,
      },

      push_new_values: {
        type: DataTypes.BOOLEAN,

        allowNull: false,
        defaultValue: false,
      },

      inherit_default_rate_card: {
        type: DataTypes.BOOLEAN,

        allowNull: false,
        defaultValue: false,
      },

      service_count: {
        type: DataTypes.INTEGER,
      },

      discipline: {
        type: DataTypes.TEXT,
      },

      output_channel: {
        type: DataTypes.TEXT,
      },

      staff_skills: {
        type: DataTypes.TEXT,
      },

      rate_card_owner: {
        type: DataTypes.TEXT,
      },

      rate_card_id: {
        type: DataTypes.TEXT,
      },

      markets: {
        type: DataTypes.TEXT,
      },

      languages: {
        type: DataTypes.TEXT,
      },

      valid_from: {
        type: DataTypes.DATEONLY,

        get: function () {
          return this.getDataValue('valid_from')
            ? moment.utc(this.getDataValue('valid_from')).format('YYYY-MM-DD')
            : null;
        },
      },

      valid_to: {
        type: DataTypes.DATEONLY,

        get: function () {
          return this.getDataValue('valid_to')
            ? moment.utc(this.getDataValue('valid_to')).format('YYYY-MM-DD')
            : null;
        },
      },

      version_number: {
        type: DataTypes.TEXT,
      },

      commercial_owner: {
        type: DataTypes.TEXT,
      },

      review_date: {
        type: DataTypes.DATEONLY,

        get: function () {
          return this.getDataValue('review_date')
            ? moment.utc(this.getDataValue('review_date')).format('YYYY-MM-DD')
            : null;
        },
      },

      assumptions: {
        type: DataTypes.TEXT,
      },

      no_fixed_text: {
        type: DataTypes.TEXT,
      },

      yes_fixed_text: {
        type: DataTypes.TEXT,
      },

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

  brand_rate_cards.associate = (db) => {
    /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity

    db.brand_rate_cards.hasMany(db.brands, {
      as: 'brands_link_rate_card',
      foreignKey: {
        name: 'link_rate_cardId',
      },
      constraints: false,
    });

    //end loop

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

    db.brand_rate_cards.belongsTo(db.brands, {
      as: 'brand',
      foreignKey: {
        name: 'brandId',
      },
      constraints: false,
    });

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

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

  return brand_rate_cards;
};
