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

project_number: {
        type: DataTypes.TEXT,
      
      

      },

name: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"planning",


"design",


"procurement",


"production",


"ready_to_ship",


"delivered",


"installed",


"closed",


"on_hold",


"cancelled"

        ],

      },

start_at: {
        type: DataTypes.DATE,
      
      

      },

due_at: {
        type: DataTypes.DATE,
      
      

      },

completed_at: {
        type: DataTypes.DATE,
      
      

      },

site_address: {
        type: DataTypes.TEXT,
      
      

      },

budget_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

contract_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  projects.associate = (db) => {


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











    db.projects.hasMany(db.project_milestones, {
      as: 'project_milestones_project',
      foreignKey: {
          name: 'projectId',
      },
      constraints: false,
    });










    db.projects.hasMany(db.purchase_orders, {
      as: 'purchase_orders_project',
      foreignKey: {
          name: 'projectId',
      },
      constraints: false,
    });




    db.projects.hasMany(db.work_orders, {
      as: 'work_orders_project',
      foreignKey: {
          name: 'projectId',
      },
      constraints: false,
    });



    db.projects.hasMany(db.deliveries, {
      as: 'deliveries_project',
      foreignKey: {
          name: 'projectId',
      },
      constraints: false,
    });



    db.projects.hasMany(db.invoices, {
      as: 'invoices_project',
      foreignKey: {
          name: 'projectId',
      },
      constraints: false,
    });






//end loop



    db.projects.belongsTo(db.customers, {
      as: 'customer',
      foreignKey: {
        name: 'customerId',
      },
      constraints: false,
    });

    db.projects.belongsTo(db.quotes, {
      as: 'quote',
      foreignKey: {
        name: 'quoteId',
      },
      constraints: false,
    });

    db.projects.belongsTo(db.users, {
      as: 'project_manager',
      foreignKey: {
        name: 'project_managerId',
      },
      constraints: false,
    });




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

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



  return projects;
};


