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

title: {
        type: DataTypes.TEXT,
      
      

      },

artist_display: {
        type: DataTypes.TEXT,
      
      

      },

bpm: {
        type: DataTypes.INTEGER,
      
      

      },

key_signature: {
        type: DataTypes.TEXT,
      
      

      },

mood: {
        type: DataTypes.ENUM,
      
      

        values: [

"happy",


"sad",


"spiritual",


"energetic",


"romantic",


"chill",


"aggressive"

        ],

      },

duration_seconds: {
        type: DataTypes.INTEGER,
      
      

      },

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

      },

lyrics: {
        type: DataTypes.TEXT,
      
      

      },

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

  songs.associate = (db) => {


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














    db.songs.hasMany(db.song_metadata, {
      as: 'song_metadata_song',
      foreignKey: {
          name: 'songId',
      },
      constraints: false,
    });


    db.songs.hasMany(db.assets, {
      as: 'assets_song',
      foreignKey: {
          name: 'songId',
      },
      constraints: false,
    });


    db.songs.hasMany(db.recording_sessions, {
      as: 'recording_sessions_song',
      foreignKey: {
          name: 'songId',
      },
      constraints: false,
    });


    db.songs.hasMany(db.tracks, {
      as: 'tracks_song',
      foreignKey: {
          name: 'songId',
      },
      constraints: false,
    });


    db.songs.hasMany(db.arrangement_sections, {
      as: 'arrangement_sections_song',
      foreignKey: {
          name: 'songId',
      },
      constraints: false,
    });


    db.songs.hasMany(db.generation_requests, {
      as: 'generation_requests_song',
      foreignKey: {
          name: 'songId',
      },
      constraints: false,
    });




    db.songs.hasMany(db.mix_sessions, {
      as: 'mix_sessions_song',
      foreignKey: {
          name: 'songId',
      },
      constraints: false,
    });


    db.songs.hasMany(db.mastering_sessions, {
      as: 'mastering_sessions_song',
      foreignKey: {
          name: 'songId',
      },
      constraints: false,
    });


    db.songs.hasMany(db.exports, {
      as: 'exports_song',
      foreignKey: {
          name: 'songId',
      },
      constraints: false,
    });


    db.songs.hasMany(db.cover_artworks, {
      as: 'cover_artworks_song',
      foreignKey: {
          name: 'songId',
      },
      constraints: false,
    });





//end loop



    db.songs.belongsTo(db.projects, {
      as: 'project',
      foreignKey: {
        name: 'projectId',
      },
      constraints: false,
    });

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

    db.songs.belongsTo(db.languages, {
      as: 'vocal_language',
      foreignKey: {
        name: 'vocal_languageId',
      },
      constraints: false,
    });

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




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

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



  return songs;
};


