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

document_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"invoice_pdf",


"guest_id",


"contract",


"approval_attachment",


"arrival_instructions",


"checklist",


"inspection_report",


"other"

        ],

      },

file_name: {
        type: DataTypes.TEXT,
      
      

      },

mime_type: {
        type: DataTypes.TEXT,
      
      

      },

file_size_bytes: {
        type: DataTypes.INTEGER,
      
      

      },

storage_key: {
        type: DataTypes.TEXT,
      
      

      },

public_url: {
        type: DataTypes.TEXT,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

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

  documents.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.documents.belongsTo(db.tenants, {
      as: 'tenant',
      foreignKey: {
        name: 'tenantId',
      },
      constraints: false,
    });

    db.documents.belongsTo(db.users, {
      as: 'uploaded_by',
      foreignKey: {
        name: 'uploaded_byId',
      },
      constraints: false,
    });

    db.documents.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });

    db.documents.belongsTo(db.booking_requests, {
      as: 'booking_request',
      foreignKey: {
        name: 'booking_requestId',
      },
      constraints: false,
    });

    db.documents.belongsTo(db.reservations, {
      as: 'reservation',
      foreignKey: {
        name: 'reservationId',
      },
      constraints: false,
    });

    db.documents.belongsTo(db.service_requests, {
      as: 'service_request',
      foreignKey: {
        name: 'service_requestId',
      },
      constraints: false,
    });

    db.documents.belongsTo(db.invoices, {
      as: 'invoice',
      foreignKey: {
        name: 'invoiceId',
      },
      constraints: false,
    });




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

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



  return documents;
};


