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

name: {
        type: DataTypes.TEXT,
      
      

      },

slug: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"trial",


"active",


"past_due",


"canceled"

        ],

      },

trial_ends_at: {
        type: DataTypes.DATE,
      
      

      },

timezone: {
        type: DataTypes.TEXT,
      
      

      },

currency: {
        type: DataTypes.TEXT,
      
      

      },

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

  workspaces.associate = (db) => {


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







    db.workspaces.hasMany(db.workspace_memberships, {
      as: 'workspace_memberships_workspace',
      foreignKey: {
          name: 'workspaceId',
      },
      constraints: false,
    });


    db.workspaces.hasMany(db.service_catalog_items, {
      as: 'service_catalog_items_workspace',
      foreignKey: {
          name: 'workspaceId',
      },
      constraints: false,
    });


    db.workspaces.hasMany(db.customers, {
      as: 'customers_workspace',
      foreignKey: {
          name: 'workspaceId',
      },
      constraints: false,
    });


    db.workspaces.hasMany(db.properties, {
      as: 'properties_workspace',
      foreignKey: {
          name: 'workspaceId',
      },
      constraints: false,
    });


    db.workspaces.hasMany(db.job_templates, {
      as: 'job_templates_workspace',
      foreignKey: {
          name: 'workspaceId',
      },
      constraints: false,
    });


    db.workspaces.hasMany(db.jobs, {
      as: 'jobs_workspace',
      foreignKey: {
          name: 'workspaceId',
      },
      constraints: false,
    });


    db.workspaces.hasMany(db.job_assignments, {
      as: 'job_assignments_workspace',
      foreignKey: {
          name: 'workspaceId',
      },
      constraints: false,
    });


    db.workspaces.hasMany(db.checklists, {
      as: 'checklists_workspace',
      foreignKey: {
          name: 'workspaceId',
      },
      constraints: false,
    });


    db.workspaces.hasMany(db.checklist_items, {
      as: 'checklist_items_workspace',
      foreignKey: {
          name: 'workspaceId',
      },
      constraints: false,
    });


    db.workspaces.hasMany(db.job_checklist_runs, {
      as: 'job_checklist_runs_workspace',
      foreignKey: {
          name: 'workspaceId',
      },
      constraints: false,
    });


    db.workspaces.hasMany(db.job_checklist_run_items, {
      as: 'job_checklist_run_items_workspace',
      foreignKey: {
          name: 'workspaceId',
      },
      constraints: false,
    });


    db.workspaces.hasMany(db.job_media, {
      as: 'job_media_workspace',
      foreignKey: {
          name: 'workspaceId',
      },
      constraints: false,
    });


    db.workspaces.hasMany(db.invoices, {
      as: 'invoices_workspace',
      foreignKey: {
          name: 'workspaceId',
      },
      constraints: false,
    });


    db.workspaces.hasMany(db.invoice_line_items, {
      as: 'invoice_line_items_workspace',
      foreignKey: {
          name: 'workspaceId',
      },
      constraints: false,
    });


    db.workspaces.hasMany(db.payments, {
      as: 'payments_workspace',
      foreignKey: {
          name: 'workspaceId',
      },
      constraints: false,
    });


    db.workspaces.hasMany(db.estimates, {
      as: 'estimates_workspace',
      foreignKey: {
          name: 'workspaceId',
      },
      constraints: false,
    });


    db.workspaces.hasMany(db.estimate_line_items, {
      as: 'estimate_line_items_workspace',
      foreignKey: {
          name: 'workspaceId',
      },
      constraints: false,
    });


    db.workspaces.hasMany(db.recurring_schedules, {
      as: 'recurring_schedules_workspace',
      foreignKey: {
          name: 'workspaceId',
      },
      constraints: false,
    });


    db.workspaces.hasMany(db.notifications, {
      as: 'notifications_workspace',
      foreignKey: {
          name: 'workspaceId',
      },
      constraints: false,
    });


    db.workspaces.hasMany(db.support_tickets, {
      as: 'support_tickets_workspace',
      foreignKey: {
          name: 'workspaceId',
      },
      constraints: false,
    });



    db.workspaces.hasMany(db.subscriptions, {
      as: 'subscriptions_workspace',
      foreignKey: {
          name: 'workspaceId',
      },
      constraints: false,
    });



//end loop



    db.workspaces.belongsTo(db.users, {
      as: 'owner',
      foreignKey: {
        name: 'ownerId',
      },
      constraints: false,
    });

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




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

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



  return workspaces;
};


