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

      name: {
        type: DataTypes.TEXT,
      },

      rate_card_import: {
        type: DataTypes.BOOLEAN,

        allowNull: false,
        defaultValue: false,
      },

      client_fixed_text: {
        type: DataTypes.TEXT,
      },

      yes_fixed_text: {
        type: DataTypes.TEXT,
      },

      default_client_contact: {
        type: DataTypes.TEXT,
      },

      inherit_default_rate_card: {
        type: DataTypes.BOOLEAN,

        allowNull: false,
        defaultValue: false,
      },

      default_costs: {
        type: DataTypes.DECIMAL,
      },

      production_fee: {
        type: DataTypes.DECIMAL,
      },

      insurance_fee: {
        type: DataTypes.DECIMAL,
      },

      temp: {
        type: DataTypes.TEXT,
      },

      xero_id: {
        type: DataTypes.TEXT,
      },

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

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

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

    //end loop

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

    db.brands.belongsTo(db.users, {
      as: 'default_account_manager',
      foreignKey: {
        name: 'default_account_managerId',
      },
      constraints: false,
    });

    db.brands.belongsTo(db.users, {
      as: 'default_sales',
      foreignKey: {
        name: 'default_salesId',
      },
      constraints: false,
    });

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

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

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

  return brands;
};
