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

title: {
        type: DataTypes.TEXT,
      
      

      },

release_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"single",


"ep",


"album",


"compilation",


"podcast"

        ],

      },

language: {
        type: DataTypes.TEXT,
      
      

      },

label_name: {
        type: DataTypes.TEXT,
      
      

      },

copyright_notice: {
        type: DataTypes.TEXT,
      
      

      },

production_notice: {
        type: DataTypes.TEXT,
      
      

      },

upc_code: {
        type: DataTypes.TEXT,
      
      

      },

upc_source: {
        type: DataTypes.ENUM,
      
      

        values: [

"auto",


"manual"

        ],

      },

release_date: {
        type: DataTypes.DATE,
      
      

      },

original_release_date: {
        type: DataTypes.DATE,
      
      

      },

moderation_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"submitted",


"in_review",


"changes_requested",


"approved",


"published",


"rejected",


"takedown_requested",


"takedown_completed"

        ],

      },

moderation_notes: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

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

      },

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

      },

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

      },

internal_catalog_code: {
        type: DataTypes.TEXT,
      
      

      },

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

  releases.associate = (db) => {

    db.releases.belongsToMany(db.distribution_platforms, {
      as: 'selected_platforms',
      foreignKey: {
        name: 'releases_selected_platformsId',
      },
      constraints: false,
      through: 'releasesSelected_platformsDistribution_platforms',
    });

    db.releases.belongsToMany(db.distribution_platforms, {
      as: 'selected_platforms_filter',
      foreignKey: {
        name: 'releases_selected_platformsId',
      },
      constraints: false,
      through: 'releasesSelected_platformsDistribution_platforms',
    });


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















    db.releases.hasMany(db.tracks, {
      as: 'tracks_release',
      foreignKey: {
          name: 'releaseId',
      },
      constraints: false,
    });



    db.releases.hasMany(db.release_deliveries, {
      as: 'release_deliveries_release',
      foreignKey: {
          name: 'releaseId',
      },
      constraints: false,
    });


    db.releases.hasMany(db.smart_links, {
      as: 'smart_links_release',
      foreignKey: {
          name: 'releaseId',
      },
      constraints: false,
    });








    db.releases.hasMany(db.royalty_lines, {
      as: 'royalty_lines_release',
      foreignKey: {
          name: 'releaseId',
      },
      constraints: false,
    });







    db.releases.hasMany(db.analytics_daily, {
      as: 'analytics_daily_release',
      foreignKey: {
          name: 'releaseId',
      },
      constraints: false,
    });














//end loop



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

    db.releases.belongsTo(db.genres, {
      as: 'primary_genre',
      foreignKey: {
        name: 'primary_genreId',
      },
      constraints: false,
    });

    db.releases.belongsTo(db.genres, {
      as: 'secondary_genre',
      foreignKey: {
        name: 'secondary_genreId',
      },
      constraints: false,
    });

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



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

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


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

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



  return releases;
};


