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

      thyme_id: {
        type: DataTypes.TEXT,
      },

      name: {
        type: DataTypes.TEXT,
      },

      office: {
        type: DataTypes.TEXT,
      },

      address: {
        type: DataTypes.TEXT,
      },

      city: {
        type: DataTypes.TEXT,
      },

      post_code: {
        type: DataTypes.TEXT,
      },

      country_txt: {
        type: DataTypes.TEXT,
      },

      phone_number: {
        type: DataTypes.TEXT,
      },

      website: {
        type: DataTypes.TEXT,
      },

      currency: {
        type: DataTypes.TEXT,
      },

      curious_customer_id: {
        type: DataTypes.TEXT,
      },

      agency: {
        type: DataTypes.TEXT,
      },

      inclusive_amends: {
        type: DataTypes.BOOLEAN,

        allowNull: false,
        defaultValue: false,
      },

      early_payment_discount: {
        type: DataTypes.DECIMAL,
      },

      client_id_link: {
        type: DataTypes.TEXT,
      },

      public_url: {
        type: DataTypes.TEXT,
      },

      production_fee: {
        type: DataTypes.DECIMAL,
      },

      insurance_fee: {
        type: DataTypes.DECIMAL,
      },

      xero_id: {
        type: DataTypes.TEXT,
      },

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

  clients.associate = (db) => {
    db.clients.belongsToMany(db.brands, {
      as: 'all_brands',
      foreignKey: {
        name: 'clients_all_brandsId',
      },
      constraints: false,
      through: 'clientsAll_brandsBrands',
    });

    db.clients.belongsToMany(db.brands, {
      as: 'all_brands_filter',
      foreignKey: {
        name: 'clients_all_brandsId',
      },
      constraints: false,
      through: 'clientsAll_brandsBrands',
    });

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

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

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

    //end loop

    db.clients.belongsTo(db.countries, {
      as: 'country',
      foreignKey: {
        name: 'countryId',
      },
      constraints: false,
    });

    db.clients.belongsTo(db.users, {
      as: 'account_manager',
      foreignKey: {
        name: 'account_managerId',
      },
      constraints: false,
    });

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

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

  return clients;
};
