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

order_number: {
        type: DataTypes.TEXT,
      
      

      },

order_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"open",


"paid",


"refunded",


"voided"

        ],

      },

placed_at: {
        type: DataTypes.DATE,
      
      

      },

subtotal_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

discount_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

tax_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

tip_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

total_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  orders.associate = (db) => {


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




















    db.orders.hasMany(db.order_items, {
      as: 'order_items_order',
      foreignKey: {
          name: 'orderId',
      },
      constraints: false,
    });


    db.orders.hasMany(db.payment_intents, {
      as: 'payment_intents_order',
      foreignKey: {
          name: 'orderId',
      },
      constraints: false,
    });













//end loop



    db.orders.belongsTo(db.locations, {
      as: 'location',
      foreignKey: {
        name: 'locationId',
      },
      constraints: false,
    });

    db.orders.belongsTo(db.registers, {
      as: 'register',
      foreignKey: {
        name: 'registerId',
      },
      constraints: false,
    });

    db.orders.belongsTo(db.shifts, {
      as: 'shift',
      foreignKey: {
        name: 'shiftId',
      },
      constraints: false,
    });

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

    db.orders.belongsTo(db.users, {
      as: 'cashier_user',
      foreignKey: {
        name: 'cashier_userId',
      },
      constraints: false,
    });

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




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

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



  return orders;
};


