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

title: {
        type: DataTypes.TEXT,
      
      

      },

track_number: {
        type: DataTypes.INTEGER,
      
      

      },

version: {
        type: DataTypes.TEXT,
      
      

      },

isrc_code: {
        type: DataTypes.TEXT,
      
      

      },

isrc_source: {
        type: DataTypes.ENUM,
      
      

        values: [

"auto",


"manual"

        ],

      },

duration_seconds: {
        type: DataTypes.INTEGER,
      
      

      },

sample_rate_hz: {
        type: DataTypes.INTEGER,
      
      

      },

bitrate_kbps: {
        type: DataTypes.INTEGER,
      
      

      },

audio_format: {
        type: DataTypes.ENUM,
      
      

        values: [

"wav",


"flac",


"mp3"

        ],

      },

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

      },

lyrics: {
        type: DataTypes.TEXT,
      
      

      },

audio_fingerprint: {
        type: DataTypes.TEXT,
      
      

      },

quality_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"pending",


"passed",


"failed",


"needs_attention"

        ],

      },

quality_notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  tracks.associate = (db) => {


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
















    db.tracks.hasMany(db.track_contributors, {
      as: 'track_contributors_track',
      foreignKey: {
          name: 'trackId',
      },
      constraints: false,
    });







    db.tracks.hasMany(db.pitch_requests, {
      as: 'pitch_requests_track',
      foreignKey: {
          name: 'trackId',
      },
      constraints: false,
    });


    db.tracks.hasMany(db.playlist_placements, {
      as: 'playlist_placements_track',
      foreignKey: {
          name: 'trackId',
      },
      constraints: false,
    });



    db.tracks.hasMany(db.royalty_lines, {
      as: 'royalty_lines_track',
      foreignKey: {
          name: 'trackId',
      },
      constraints: false,
    });







    db.tracks.hasMany(db.analytics_daily, {
      as: 'analytics_daily_track',
      foreignKey: {
          name: 'trackId',
      },
      constraints: false,
    });














//end loop



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

    db.tracks.belongsTo(db.genres, {
      as: 'genre',
      foreignKey: {
        name: 'genreId',
      },
      constraints: false,
    });

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



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


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

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



  return tracks;
};


