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,
      
      

      },

summary: {
        type: DataTypes.TEXT,
      
      

      },

project_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"startup",


"acquisition",


"partnership",


"expansion"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"idea",


"research",


"planning",


"fundraising",


"negotiation",


"launched",


"archived"

        ],

      },

industry: {
        type: DataTypes.TEXT,
      
      

      },

target_market: {
        type: DataTypes.TEXT,
      
      

      },

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

  projects.associate = (db) => {

    db.projects.belongsToMany(db.source_items, {
      as: 'source_items',
      foreignKey: {
        name: 'projects_source_itemsId',
      },
      constraints: false,
      through: 'projectsSource_itemsSource_items',
    });

    db.projects.belongsToMany(db.source_items, {
      as: 'source_items_filter',
      foreignKey: {
        name: 'projects_source_itemsId',
      },
      constraints: false,
      through: 'projectsSource_itemsSource_items',
    });

    db.projects.belongsToMany(db.mind_maps, {
      as: 'mind_maps',
      foreignKey: {
        name: 'projects_mind_mapsId',
      },
      constraints: false,
      through: 'projectsMind_mapsMind_maps',
    });

    db.projects.belongsToMany(db.mind_maps, {
      as: 'mind_maps_filter',
      foreignKey: {
        name: 'projects_mind_mapsId',
      },
      constraints: false,
      through: 'projectsMind_mapsMind_maps',
    });

    db.projects.belongsToMany(db.ai_runs, {
      as: 'ai_runs',
      foreignKey: {
        name: 'projects_ai_runsId',
      },
      constraints: false,
      through: 'projectsAi_runsAi_runs',
    });

    db.projects.belongsToMany(db.ai_runs, {
      as: 'ai_runs_filter',
      foreignKey: {
        name: 'projects_ai_runsId',
      },
      constraints: false,
      through: 'projectsAi_runsAi_runs',
    });

    db.projects.belongsToMany(db.business_plan_versions, {
      as: 'business_plan_versions',
      foreignKey: {
        name: 'projects_business_plan_versionsId',
      },
      constraints: false,
      through: 'projectsBusiness_plan_versionsBusiness_plan_versions',
    });

    db.projects.belongsToMany(db.business_plan_versions, {
      as: 'business_plan_versions_filter',
      foreignKey: {
        name: 'projects_business_plan_versionsId',
      },
      constraints: false,
      through: 'projectsBusiness_plan_versionsBusiness_plan_versions',
    });

    db.projects.belongsToMany(db.deal_packages, {
      as: 'deal_packages',
      foreignKey: {
        name: 'projects_deal_packagesId',
      },
      constraints: false,
      through: 'projectsDeal_packagesDeal_packages',
    });

    db.projects.belongsToMany(db.deal_packages, {
      as: 'deal_packages_filter',
      foreignKey: {
        name: 'projects_deal_packagesId',
      },
      constraints: false,
      through: 'projectsDeal_packagesDeal_packages',
    });


/// 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.mind_maps, {
      as: 'mind_maps_project',
      foreignKey: {
          name: 'projectId',
      },
      constraints: false,
    });




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



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


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


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


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



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



//end loop



    db.projects.belongsTo(db.workspaces, {
      as: 'workspace',
      foreignKey: {
        name: 'workspaceId',
      },
      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;
};


