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

name: {
        type: DataTypes.TEXT,
      
      

      },

target_platform: {
        type: DataTypes.ENUM,
      
      

        values: [

"android_apk",


"android_aab",


"docker_image",


"web_preview"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"queued",


"running",


"succeeded",


"failed",


"canceled"

        ],

      },

queued_at: {
        type: DataTypes.DATE,
      
      

      },

started_at: {
        type: DataTypes.DATE,
      
      

      },

finished_at: {
        type: DataTypes.DATE,
      
      

      },

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

  build_jobs.associate = (db) => {

    db.build_jobs.belongsToMany(db.build_artifacts, {
      as: 'artifacts',
      foreignKey: {
        name: 'build_jobs_artifactsId',
      },
      constraints: false,
      through: 'build_jobsArtifactsBuild_artifacts',
    });

    db.build_jobs.belongsToMany(db.build_artifacts, {
      as: 'artifacts_filter',
      foreignKey: {
        name: 'build_jobs_artifactsId',
      },
      constraints: false,
      through: 'build_jobsArtifactsBuild_artifacts',
    });


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























    db.build_jobs.hasMany(db.build_artifacts, {
      as: 'build_artifacts_build_job',
      foreignKey: {
          name: 'build_jobId',
      },
      constraints: false,
    });







//end loop



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

    db.build_jobs.belongsTo(db.users, {
      as: 'user',
      foreignKey: {
        name: 'userId',
      },
      constraints: false,
    });

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

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




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

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



  return build_jobs;
};


