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

title: {
        type: DataTypes.TEXT,
      
      

      },

scope: {
        type: DataTypes.ENUM,
      
      

        values: [

"uzbekistan",


"global",


"local"

        ],

      },

category: {
        type: DataTypes.ENUM,
      
      

        values: [

"curriculum",


"teacher_training",


"assessment",


"inequality",


"infrastructure",


"digital_access",


"motivation",


"mental_health",


"other"

        ],

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

evidence_links: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"submitted",


"under_review",


"published",


"rejected"

        ],

      },

submitted_at: {
        type: DataTypes.DATE,
      
      

      },

published_at: {
        type: DataTypes.DATE,
      
      

      },

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

  problem_reports.associate = (db) => {


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






















    db.problem_reports.hasMany(db.solution_proposals, {
      as: 'solution_proposals_problem_report',
      foreignKey: {
          name: 'problem_reportId',
      },
      constraints: false,
    });


    db.problem_reports.hasMany(db.votes, {
      as: 'votes_problem_report',
      foreignKey: {
          name: 'problem_reportId',
      },
      constraints: false,
    });


    db.problem_reports.hasMany(db.comments, {
      as: 'comments_problem_report',
      foreignKey: {
          name: 'problem_reportId',
      },
      constraints: false,
    });









//end loop



    db.problem_reports.belongsTo(db.users, {
      as: 'author',
      foreignKey: {
        name: 'authorId',
      },
      constraints: false,
    });

    db.problem_reports.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });



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

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


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

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



  return problem_reports;
};


