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

tenant_name: {
        type: DataTypes.TEXT,
      
      

      },

slug: {
        type: DataTypes.TEXT,
      
      

      },

tenant_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"farm",


"buyer",


"investor",


"admin"

        ],

      },

contact_email: {
        type: DataTypes.TEXT,
      
      

      },

contact_phone: {
        type: DataTypes.TEXT,
      
      

      },

billing_address: {
        type: DataTypes.TEXT,
      
      

      },

subscription_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"trial",


"active",


"past_due",


"canceled"

        ],

      },

trial_ends_at: {
        type: DataTypes.DATE,
      
      

      },

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

      },

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

  tenants.associate = (db) => {


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







    db.tenants.hasMany(db.user_memberships, {
      as: 'user_memberships_tenant',
      foreignKey: {
          name: 'tenantId',
      },
      constraints: false,
    });


    db.tenants.hasMany(db.tenant_locations, {
      as: 'tenant_locations_tenant',
      foreignKey: {
          name: 'tenantId',
      },
      constraints: false,
    });


    db.tenants.hasMany(db.ponds, {
      as: 'ponds_tenant',
      foreignKey: {
          name: 'tenantId',
      },
      constraints: false,
    });



    db.tenants.hasMany(db.batches, {
      as: 'batches_tenant',
      foreignKey: {
          name: 'tenantId',
      },
      constraints: false,
    });


    db.tenants.hasMany(db.feed_products, {
      as: 'feed_products_tenant',
      foreignKey: {
          name: 'tenantId',
      },
      constraints: false,
    });


    db.tenants.hasMany(db.inventory_items, {
      as: 'inventory_items_tenant',
      foreignKey: {
          name: 'tenantId',
      },
      constraints: false,
    });


    db.tenants.hasMany(db.inventory_movements, {
      as: 'inventory_movements_tenant',
      foreignKey: {
          name: 'tenantId',
      },
      constraints: false,
    });


    db.tenants.hasMany(db.feeding_logs, {
      as: 'feeding_logs_tenant',
      foreignKey: {
          name: 'tenantId',
      },
      constraints: false,
    });


    db.tenants.hasMany(db.water_quality_logs, {
      as: 'water_quality_logs_tenant',
      foreignKey: {
          name: 'tenantId',
      },
      constraints: false,
    });


    db.tenants.hasMany(db.health_events, {
      as: 'health_events_tenant',
      foreignKey: {
          name: 'tenantId',
      },
      constraints: false,
    });


    db.tenants.hasMany(db.harvests, {
      as: 'harvests_tenant',
      foreignKey: {
          name: 'tenantId',
      },
      constraints: false,
    });


    db.tenants.hasMany(db.marketplace_listings, {
      as: 'marketplace_listings_seller_tenant',
      foreignKey: {
          name: 'seller_tenantId',
      },
      constraints: false,
    });


    db.tenants.hasMany(db.orders, {
      as: 'orders_buyer_tenant',
      foreignKey: {
          name: 'buyer_tenantId',
      },
      constraints: false,
    });

    db.tenants.hasMany(db.orders, {
      as: 'orders_seller_tenant',
      foreignKey: {
          name: 'seller_tenantId',
      },
      constraints: false,
    });



    db.tenants.hasMany(db.payment_transactions, {
      as: 'payment_transactions_buyer_tenant',
      foreignKey: {
          name: 'buyer_tenantId',
      },
      constraints: false,
    });

    db.tenants.hasMany(db.payment_transactions, {
      as: 'payment_transactions_seller_tenant',
      foreignKey: {
          name: 'seller_tenantId',
      },
      constraints: false,
    });



    db.tenants.hasMany(db.investments, {
      as: 'investments_investor_tenant',
      foreignKey: {
          name: 'investor_tenantId',
      },
      constraints: false,
    });

    db.tenants.hasMany(db.investments, {
      as: 'investments_farm_tenant',
      foreignKey: {
          name: 'farm_tenantId',
      },
      constraints: false,
    });


    db.tenants.hasMany(db.api_keys, {
      as: 'api_keys_tenant',
      foreignKey: {
          name: 'tenantId',
      },
      constraints: false,
    });


    db.tenants.hasMany(db.webhook_endpoints, {
      as: 'webhook_endpoints_tenant',
      foreignKey: {
          name: 'tenantId',
      },
      constraints: false,
    });



    db.tenants.hasMany(db.email_notifications, {
      as: 'email_notifications_tenant',
      foreignKey: {
          name: 'tenantId',
      },
      constraints: false,
    });


    db.tenants.hasMany(db.audit_logs, {
      as: 'audit_logs_tenant',
      foreignKey: {
          name: 'tenantId',
      },
      constraints: false,
    });


    db.tenants.hasMany(db.csv_jobs, {
      as: 'csv_jobs_tenant',
      foreignKey: {
          name: 'tenantId',
      },
      constraints: false,
    });



//end loop



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




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

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



  return tenants;
};


