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

billing_provider: {
        type: DataTypes.ENUM,
      
      

        values: [

"razorpay",


"stripe"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"trialing",


"active",


"past_due",


"paused",


"cancelled",


"expired"

        ],

      },

current_period_start_at: {
        type: DataTypes.DATE,
      
      

      },

current_period_end_at: {
        type: DataTypes.DATE,
      
      

      },

trial_end_at: {
        type: DataTypes.DATE,
      
      

      },

provider_customer_key: {
        type: DataTypes.TEXT,
      
      

      },

provider_subscription_key: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

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

  company_subscriptions.associate = (db) => {


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




















    db.company_subscriptions.hasMany(db.payments, {
      as: 'payments_company_subscription',
      foreignKey: {
          name: 'company_subscriptionId',
      },
      constraints: false,
    });











//end loop



    db.company_subscriptions.belongsTo(db.companies, {
      as: 'company',
      foreignKey: {
        name: 'companyId',
      },
      constraints: false,
    });

    db.company_subscriptions.belongsTo(db.subscription_plans, {
      as: 'plan',
      foreignKey: {
        name: 'planId',
      },
      constraints: false,
    });

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




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

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



  return company_subscriptions;
};


