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

period_start_at: {
        type: DataTypes.DATE,
      
      

      },

period_end_at: {
        type: DataTypes.DATE,
      
      

      },

source_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"api",


"csv",


"xml",


"manual"

        ],

      },

source_reference: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"imported",


"processing",


"completed",


"failed"

        ],

      },

error_details: {
        type: DataTypes.TEXT,
      
      

      },

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

  royalty_reports.associate = (db) => {


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

























    db.royalty_reports.hasMany(db.royalty_lines, {
      as: 'royalty_lines_royalty_report',
      foreignKey: {
          name: 'royalty_reportId',
      },
      constraints: false,
    });




















//end loop



    db.royalty_reports.belongsTo(db.artist_profiles, {
      as: 'artist_profile',
      foreignKey: {
        name: 'artist_profileId',
      },
      constraints: false,
    });

    db.royalty_reports.belongsTo(db.distribution_platforms, {
      as: 'platform',
      foreignKey: {
        name: 'platformId',
      },
      constraints: false,
    });

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



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


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

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



  return royalty_reports;
};


