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

title: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"scheduled",


"in_progress",


"completed",


"canceled",


"no_show"

        ],

      },

start_at: {
        type: DataTypes.DATE,
      
      

      },

end_at: {
        type: DataTypes.DATE,
      
      

      },

estimated_duration_minutes: {
        type: DataTypes.INTEGER,
      
      

      },

price: {
        type: DataTypes.DECIMAL,
      
      

      },

instructions: {
        type: DataTypes.TEXT,
      
      

      },

internal_notes: {
        type: DataTypes.TEXT,
      
      

      },

requires_invoice: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

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

  jobs.associate = (db) => {


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













    db.jobs.hasMany(db.job_assignments, {
      as: 'job_assignments_job',
      foreignKey: {
          name: 'jobId',
      },
      constraints: false,
    });




    db.jobs.hasMany(db.job_checklist_runs, {
      as: 'job_checklist_runs_job',
      foreignKey: {
          name: 'jobId',
      },
      constraints: false,
    });



    db.jobs.hasMany(db.job_media, {
      as: 'job_media_job',
      foreignKey: {
          name: 'jobId',
      },
      constraints: false,
    });


    db.jobs.hasMany(db.invoices, {
      as: 'invoices_job',
      foreignKey: {
          name: 'jobId',
      },
      constraints: false,
    });












//end loop



    db.jobs.belongsTo(db.workspaces, {
      as: 'workspace',
      foreignKey: {
        name: 'workspaceId',
      },
      constraints: false,
    });

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

    db.jobs.belongsTo(db.properties, {
      as: 'property',
      foreignKey: {
        name: 'propertyId',
      },
      constraints: false,
    });

    db.jobs.belongsTo(db.service_catalog_items, {
      as: 'service_catalog_item',
      foreignKey: {
        name: 'service_catalog_itemId',
      },
      constraints: false,
    });

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




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

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



  return jobs;
};


