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

checklist_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"pre_arrival",


"check_in",


"check_out",


"turnover"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"not_started",


"in_progress",


"completed",


"blocked"

        ],

      },

due_at: {
        type: DataTypes.DATE,
      
      

      },

completed_at: {
        type: DataTypes.DATE,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  checklists.associate = (db) => {

    db.checklists.belongsToMany(db.checklist_items, {
      as: 'items',
      foreignKey: {
        name: 'checklists_itemsId',
      },
      constraints: false,
      through: 'checklistsItemsChecklist_items',
    });

    db.checklists.belongsToMany(db.checklist_items, {
      as: 'items_filter',
      foreignKey: {
        name: 'checklists_itemsId',
      },
      constraints: false,
      through: 'checklistsItemsChecklist_items',
    });


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





























    db.checklists.hasMany(db.checklist_items, {
      as: 'checklist_items_checklist',
      foreignKey: {
          name: 'checklistId',
      },
      constraints: false,
    });




//end loop



    db.checklists.belongsTo(db.tenants, {
      as: 'tenant',
      foreignKey: {
        name: 'tenantId',
      },
      constraints: false,
    });

    db.checklists.belongsTo(db.reservations, {
      as: 'reservation',
      foreignKey: {
        name: 'reservationId',
      },
      constraints: false,
    });

    db.checklists.belongsTo(db.users, {
      as: 'assigned_to',
      foreignKey: {
        name: 'assigned_toId',
      },
      constraints: false,
    });

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




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

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



  return checklists;
};


