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

name: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

pipeline_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"apk_build",


"test",


"deploy",


"full_ci_cd"

        ],

      },

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

      },

config_yaml: {
        type: DataTypes.TEXT,
      
      

      },

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

  build_pipelines.associate = (db) => {

    db.build_pipelines.belongsToMany(db.build_jobs, {
      as: 'jobs',
      foreignKey: {
        name: 'build_pipelines_jobsId',
      },
      constraints: false,
      through: 'build_pipelinesJobsBuild_jobs',
    });

    db.build_pipelines.belongsToMany(db.build_jobs, {
      as: 'jobs_filter',
      foreignKey: {
        name: 'build_pipelines_jobsId',
      },
      constraints: false,
      through: 'build_pipelinesJobsBuild_jobs',
    });


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





















    db.build_pipelines.hasMany(db.build_jobs, {
      as: 'build_jobs_pipeline',
      foreignKey: {
          name: 'pipelineId',
      },
      constraints: false,
    });









//end loop



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




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

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



  return build_pipelines;
};


