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

order_number: {
        type: DataTypes.TEXT,
      
      

      },

source: {
        type: DataTypes.ENUM,
      
      

        values: [

"public_web",


"qr",


"chatbot",


"admin_panel"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"new",


"diagnosing",


"awaiting_approval",


"approved",


"in_repair",


"ready_for_pickup",


"delivered",


"cancelled"

        ],

      },

problem_description: {
        type: DataTypes.TEXT,
      
      

      },

diagnosis_notes: {
        type: DataTypes.TEXT,
      
      

      },

internal_notes: {
        type: DataTypes.TEXT,
      
      

      },

estimated_cost: {
        type: DataTypes.DECIMAL,
      
      

      },

final_cost: {
        type: DataTypes.DECIMAL,
      
      

      },

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

      },

deposit_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

requested_at: {
        type: DataTypes.DATE,
      
      

      },

received_at: {
        type: DataTypes.DATE,
      
      

      },

estimated_ready_at: {
        type: DataTypes.DATE,
      
      

      },

completed_at: {
        type: DataTypes.DATE,
      
      

      },

delivered_at: {
        type: DataTypes.DATE,
      
      

      },

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

      },

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

  repair_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.repair_orders.hasMany(db.order_items, {
      as: 'order_items_repair_order',
      foreignKey: {
          name: 'repair_orderId',
      },
      constraints: false,
    });



    db.repair_orders.hasMany(db.payments, {
      as: 'payments_repair_order',
      foreignKey: {
          name: 'repair_orderId',
      },
      constraints: false,
    });


    db.repair_orders.hasMany(db.invoices, {
      as: 'invoices_repair_order',
      foreignKey: {
          name: 'repair_orderId',
      },
      constraints: false,
    });



    db.repair_orders.hasMany(db.whatsapp_notifications, {
      as: 'whatsapp_notifications_repair_order',
      foreignKey: {
          name: 'repair_orderId',
      },
      constraints: false,
    });






//end loop



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

    db.repair_orders.belongsTo(db.devices, {
      as: 'device',
      foreignKey: {
        name: 'deviceId',
      },
      constraints: false,
    });



    db.repair_orders.hasMany(db.file, {
      as: 'intake_photos',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.repair_orders.getTableName(),
        belongsToColumn: 'intake_photos',
      },
    });

    db.repair_orders.hasMany(db.file, {
      as: 'attachments',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.repair_orders.getTableName(),
        belongsToColumn: 'attachments',
      },
    });


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

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



  return repair_orders;
};


