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

run_at: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"in_progress",


"completed",


"cancelled"

        ],

      },

general_notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  checklist_runs.associate = (db) => {


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







































    db.checklist_runs.hasMany(db.checklist_run_items, {
      as: 'checklist_run_items_checklist_run',
      foreignKey: {
          name: 'checklist_runId',
      },
      constraints: false,
    });




















//end loop



    db.checklist_runs.belongsTo(db.branches, {
      as: 'branch',
      foreignKey: {
        name: 'branchId',
      },
      constraints: false,
    });

    db.checklist_runs.belongsTo(db.store_checklists, {
      as: 'checklist',
      foreignKey: {
        name: 'checklistId',
      },
      constraints: false,
    });

    db.checklist_runs.belongsTo(db.users, {
      as: 'completed_by',
      foreignKey: {
        name: 'completed_byId',
      },
      constraints: false,
    });

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




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

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



  return checklist_runs;
};


