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

title: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"open",


"in_progress",


"waiting_parts",


"completed",


"canceled"

        ],

      },

priority: {
        type: DataTypes.ENUM,
      
      

        values: [

"low",


"medium",


"high",


"urgent"

        ],

      },

reported_at: {
        type: DataTypes.DATE,
      
      

      },

due_at: {
        type: DataTypes.DATE,
      
      

      },

completed_at: {
        type: DataTypes.DATE,
      
      

      },

estimated_cost: {
        type: DataTypes.DECIMAL,
      
      

      },

actual_cost: {
        type: DataTypes.DECIMAL,
      
      

      },

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

  work_orders.associate = (db) => {


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
































//end loop



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

    db.work_orders.belongsTo(db.assets, {
      as: 'asset',
      foreignKey: {
        name: 'assetId',
      },
      constraints: false,
    });

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



    db.work_orders.hasMany(db.file, {
      as: 'attachments_images',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.work_orders.getTableName(),
        belongsToColumn: 'attachments_images',
      },
    });

    db.work_orders.hasMany(db.file, {
      as: 'attachments_files',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.work_orders.getTableName(),
        belongsToColumn: 'attachments_files',
      },
    });


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

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



  return work_orders;
};


