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

channel_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"direct",


"ota",


"phone",


"walk_in",


"corporate"

        ],

      },

name: {
        type: DataTypes.TEXT,
      
      

      },

external_reference: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

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

  sales_channels.associate = (db) => {


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


















    db.sales_channels.hasMany(db.ota_connections, {
      as: 'ota_connections_sales_channel',
      foreignKey: {
          name: 'sales_channelId',
      },
      constraints: false,
    });


    db.sales_channels.hasMany(db.reservations, {
      as: 'reservations_sales_channel',
      foreignKey: {
          name: 'sales_channelId',
      },
      constraints: false,
    });
















//end loop



    db.sales_channels.belongsTo(db.hotels, {
      as: 'hotel',
      foreignKey: {
        name: 'hotelId',
      },
      constraints: false,
    });

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




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

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



  return sales_channels;
};


