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_channel: {
        type: DataTypes.ENUM,
      
      

        values: [

"sur_place_qr",


"a_distance"

        ],

      },

fulfillment_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"sur_place",


"emporter",


"livraison"

        ],

      },

delivery_address: {
        type: DataTypes.TEXT,
      
      

      },

delivery_latitude: {
        type: DataTypes.DECIMAL,
      
      

      },

delivery_longitude: {
        type: DataTypes.DECIMAL,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"brouillon",


"validee",


"en_preparation",


"prete",


"servie",


"annulee"

        ],

      },

ordered_at: {
        type: DataTypes.DATE,
      
      

      },

requested_for: {
        type: DataTypes.DATE,
      
      

      },

customer_note: {
        type: DataTypes.TEXT,
      
      

      },

subtotal_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

total_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

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

      },

      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.payments, {
      as: 'payments_order',
      foreignKey: {
          name: 'orderId',
      },
      constraints: false,
    });





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






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




//end loop



    db.orders.belongsTo(db.restaurants, {
      as: 'restaurant',
      foreignKey: {
        name: 'restaurantId',
      },
      constraints: false,
    });

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

    db.orders.belongsTo(db.restaurant_tables, {
      as: 'table',
      foreignKey: {
        name: 'tableId',
      },
      constraints: false,
    });




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

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



  return orders;
};


