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

serial_number: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"in_stock",


"allocated",


"sold",


"returned",


"scrapped"

        ],

      },

received_at: {
        type: DataTypes.DATE,
      
      

      },

sold_at: {
        type: DataTypes.DATE,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  serial_numbers.associate = (db) => {


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
























    db.serial_numbers.hasMany(db.inventory_transfer_lines, {
      as: 'inventory_transfer_lines_serial_number',
      foreignKey: {
          name: 'serial_numberId',
      },
      constraints: false,
    });


    db.serial_numbers.hasMany(db.stock_movements, {
      as: 'stock_movements_serial_number',
      foreignKey: {
          name: 'serial_numberId',
      },
      constraints: false,
    });






    db.serial_numbers.hasMany(db.sales_allocations, {
      as: 'sales_allocations_serial_number',
      foreignKey: {
          name: 'serial_numberId',
      },
      constraints: false,
    });





    db.serial_numbers.hasMany(db.customer_return_lines, {
      as: 'customer_return_lines_serial_number',
      foreignKey: {
          name: 'serial_numberId',
      },
      constraints: false,
    });





    db.serial_numbers.hasMany(db.receipt_lines, {
      as: 'receipt_lines_serial_number',
      foreignKey: {
          name: 'serial_numberId',
      },
      constraints: false,
    });



















//end loop



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

    db.serial_numbers.belongsTo(db.items, {
      as: 'item',
      foreignKey: {
        name: 'itemId',
      },
      constraints: false,
    });

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




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

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



  return serial_numbers;
};


