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

title: {
        type: DataTypes.TEXT,
      
      

      },

as_a: {
        type: DataTypes.TEXT,
      
      

      },

i_want: {
        type: DataTypes.TEXT,
      
      

      },

so_that: {
        type: DataTypes.TEXT,
      
      

      },

template_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"feature_story",


"bug_fix",


"tech_debt",


"spike"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"ready",


"in_progress",


"done"

        ],

      },

board_column: {
        type: DataTypes.ENUM,
      
      

        values: [

"backlog",


"todo",


"in_progress",


"review",


"done"

        ],

      },

priority: {
        type: DataTypes.ENUM,
      
      

        values: [

"low",


"medium",


"high",


"critical"

        ],

      },

quality_score: {
        type: DataTypes.INTEGER,
      
      

      },

quality_suggestions: {
        type: DataTypes.TEXT,
      
      

      },

tags_csv: {
        type: DataTypes.TEXT,
      
      

      },

created_at_time: {
        type: DataTypes.DATE,
      
      

      },

updated_at_time: {
        type: DataTypes.DATE,
      
      

      },

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

  user_stories.associate = (db) => {


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










    db.user_stories.hasMany(db.acceptance_criteria, {
      as: 'acceptance_criteria_story',
      foreignKey: {
          name: 'storyId',
      },
      constraints: false,
    });





    db.user_stories.hasMany(db.story_document_links, {
      as: 'story_document_links_story',
      foreignKey: {
          name: 'storyId',
      },
      constraints: false,
    });


    db.user_stories.hasMany(db.collaboration_sessions, {
      as: 'collaboration_sessions_story',
      foreignKey: {
          name: 'storyId',
      },
      constraints: false,
    });


    db.user_stories.hasMany(db.comment_threads, {
      as: 'comment_threads_story',
      foreignKey: {
          name: 'storyId',
      },
      constraints: false,
    });






    db.user_stories.hasMany(db.approvals, {
      as: 'approvals_story',
      foreignKey: {
          name: 'storyId',
      },
      constraints: false,
    });






    db.user_stories.hasMany(db.sprint_items, {
      as: 'sprint_items_story',
      foreignKey: {
          name: 'storyId',
      },
      constraints: false,
    });


    db.user_stories.hasMany(db.blockers, {
      as: 'blockers_story',
      foreignKey: {
          name: 'storyId',
      },
      constraints: false,
    });



    db.user_stories.hasMany(db.ai_generations, {
      as: 'ai_generations_story',
      foreignKey: {
          name: 'storyId',
      },
      constraints: false,
    });



//end loop



    db.user_stories.belongsTo(db.teams, {
      as: 'team',
      foreignKey: {
        name: 'teamId',
      },
      constraints: false,
    });

    db.user_stories.belongsTo(db.users, {
      as: 'assignee',
      foreignKey: {
        name: 'assigneeId',
      },
      constraints: false,
    });

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




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

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



  return user_stories;
};


