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

name: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"in_progress",


"ready_for_mix",


"ready_for_master",


"exported",


"archived"

        ],

      },

target_bpm: {
        type: DataTypes.INTEGER,
      
      

      },

target_key: {
        type: DataTypes.TEXT,
      
      

      },

mood: {
        type: DataTypes.ENUM,
      
      

        values: [

"happy",


"sad",


"spiritual",


"energetic",


"romantic",


"chill",


"aggressive"

        ],

      },

target_duration_seconds: {
        type: DataTypes.INTEGER,
      
      

      },

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

      },

last_opened_at: {
        type: DataTypes.DATE,
      
      

      },

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

  projects.associate = (db) => {


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












    db.projects.hasMany(db.project_collaborations, {
      as: 'project_collaborations_project',
      foreignKey: {
          name: 'projectId',
      },
      constraints: false,
    });


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



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


    db.projects.hasMany(db.recording_sessions, {
      as: 'recording_sessions_project',
      foreignKey: {
          name: 'projectId',
      },
      constraints: false,
    });




    db.projects.hasMany(db.generation_requests, {
      as: 'generation_requests_project',
      foreignKey: {
          name: 'projectId',
      },
      constraints: false,
    });











//end loop



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

    db.projects.belongsTo(db.users, {
      as: 'created_user',
      foreignKey: {
        name: 'created_userId',
      },
      constraints: false,
    });

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

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




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

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



  return projects;
};


