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

asset_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"audio",


"midi",


"image",


"document",


"preset"

        ],

      },

audio_role: {
        type: DataTypes.ENUM,
      
      

        values: [

"vocal_raw",


"vocal_processed",


"instrumental",


"drums",


"bass",


"instruments",


"mix",


"master",


"stem",


"reference"

        ],

      },

name: {
        type: DataTypes.TEXT,
      
      

      },

duration_seconds: {
        type: DataTypes.DECIMAL,
      
      

      },

sample_rate_hz: {
        type: DataTypes.INTEGER,
      
      

      },

bit_depth: {
        type: DataTypes.INTEGER,
      
      

      },

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

      },

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

  assets.associate = (db) => {


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

















    db.assets.hasMany(db.tracks, {
      as: 'tracks_source_asset',
      foreignKey: {
          name: 'source_assetId',
      },
      constraints: false,
    });



    db.assets.hasMany(db.generation_requests, {
      as: 'generation_requests_input_asset',
      foreignKey: {
          name: 'input_assetId',
      },
      constraints: false,
    });

    db.assets.hasMany(db.generation_requests, {
      as: 'generation_requests_output_asset',
      foreignKey: {
          name: 'output_assetId',
      },
      constraints: false,
    });




    db.assets.hasMany(db.mix_sessions, {
      as: 'mix_sessions_result_mix_asset',
      foreignKey: {
          name: 'result_mix_assetId',
      },
      constraints: false,
    });


    db.assets.hasMany(db.mastering_sessions, {
      as: 'mastering_sessions_input_mix_asset',
      foreignKey: {
          name: 'input_mix_assetId',
      },
      constraints: false,
    });

    db.assets.hasMany(db.mastering_sessions, {
      as: 'mastering_sessions_result_master_asset',
      foreignKey: {
          name: 'result_master_assetId',
      },
      constraints: false,
    });


    db.assets.hasMany(db.exports, {
      as: 'exports_export_asset',
      foreignKey: {
          name: 'export_assetId',
      },
      constraints: false,
    });



    db.assets.hasMany(db.marketplace_listings, {
      as: 'marketplace_listings_preview_asset',
      foreignKey: {
          name: 'preview_assetId',
      },
      constraints: false,
    });

    db.assets.hasMany(db.marketplace_listings, {
      as: 'marketplace_listings_deliverable_asset',
      foreignKey: {
          name: 'deliverable_assetId',
      },
      constraints: false,
    });




//end loop



    db.assets.belongsTo(db.workspaces, {
      as: 'workspace',
      foreignKey: {
        name: 'workspaceId',
      },
      constraints: false,
    });

    db.assets.belongsTo(db.users, {
      as: 'uploaded_user',
      foreignKey: {
        name: 'uploaded_userId',
      },
      constraints: false,
    });

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

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

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



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

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


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

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



  return assets;
};


