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

      invoice_number: {
        type: DataTypes.TEXT,
      },

      amount_mad: {
        type: DataTypes.DECIMAL,
      },

      status: {
        type: DataTypes.ENUM,

        values: ['draft', 'sent', 'paid', 'void'],
      },

      issued_at: {
        type: DataTypes.DATE,
      },

      pdf_url: {
        type: DataTypes.TEXT,
      },

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

  invoices_saas.associate = (db) => {
    /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity

    //end loop

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

    db.invoices_saas.belongsTo(db.subscriptions, {
      as: 'subscription',
      foreignKey: {
        name: 'subscriptionId',
      },
      constraints: false,
    });

    db.invoices_saas.belongsTo(db.agences, {
      as: 'agences',
      foreignKey: {
        name: 'agencesId',
      },
      constraints: false,
    });

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

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

  return invoices_saas;
};
