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

week_start_at: {
        type: DataTypes.DATE,
      
      

      },

week_end_at: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"submitted",


"approved",


"rejected"

        ],

      },

submitted_at: {
        type: DataTypes.DATE,
      
      

      },

approved_at: {
        type: DataTypes.DATE,
      
      

      },

approval_notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  timesheets.associate = (db) => {


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































    db.timesheets.hasMany(db.time_entries, {
      as: 'time_entries_timesheet',
      foreignKey: {
          name: 'timesheetId',
      },
      constraints: false,
    });
















//end loop



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

    db.timesheets.belongsTo(db.users, {
      as: 'user',
      foreignKey: {
        name: 'userId',
      },
      constraints: false,
    });

    db.timesheets.belongsTo(db.users, {
      as: 'approver',
      foreignKey: {
        name: 'approverId',
      },
      constraints: false,
    });

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




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

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



  return timesheets;
};


