const { empty } = require("../Helpers/Common");
const sequelize = require("../config/dbconnection");
const Sequelize = require('sequelize')


const PartnerStage = sequelize.define('partner_stages', {
  id: { type: Sequelize.INTEGER, allowNull: false, primaryKey: true, autoIncrement: true },
  partner_id: { type: Sequelize.INTEGER, allowNull: false },
  workflow_id: { type: Sequelize.INTEGER, allowNull: false },
  stage_id: { type: Sequelize.INTEGER, allowNull: false },
  stage_type: { type: Sequelize.TINYINT, allowNull: false },
  position_order:{type:Sequelize.TINYINT,allowNull:false,defaultValue:null},
  stage_submit:{type:Sequelize.TINYINT,allowNull:false,defaultValue:1},
  added_by:{type:Sequelize.INTEGER,allowNull:false,defaultValue:null},
  timestamp:{type:Sequelize.INTEGER,allowNull:false,defaultValue:null},
  updated_on:{type:Sequelize.INTEGER,allowNull:false,defaultValue:0},
  approved_on:{type:Sequelize.INTEGER,allowNull:true,defaultValue:null},
  approved_by:{type:Sequelize.INTEGER,allowNull:true,defaultValue:null},
  status:{type:Sequelize.TINYINT,allowNull:false,defaultValue:1},
},{timestamps:false})
const PartnerTeam = sequelize.define('partner_relationship_managers', {
  id: { type: Sequelize.INTEGER, allowNull: false, primaryKey: true, autoIncrement: true },
  partner_id: { type: Sequelize.INTEGER, allowNull: false },
  team_id: { type: Sequelize.INTEGER, allowNull: false },
  added_by:{type:Sequelize.INTEGER,allowNull:false,defaultValue:null},
  timestamp:{type:Sequelize.INTEGER,allowNull:false,defaultValue:null},
  status:{type:Sequelize.TINYINT,allowNull:false,defaultValue:1},
},{timestamps:false})
const RfqStage = sequelize.define('rfq_stages', {
  id: { type: Sequelize.INTEGER, allowNull: false, primaryKey: true, autoIncrement: true },
  request_id: { type: Sequelize.INTEGER, allowNull: false },
  partner_id: { type: Sequelize.INTEGER, allowNull: false },
  workflow_id: { type: Sequelize.INTEGER, allowNull: false },
  stage_id: { type: Sequelize.INTEGER, allowNull: false },
  stage_type: { type: Sequelize.TINYINT, allowNull: false },
  position_order:{type:Sequelize.TINYINT,allowNull:false,defaultValue:null},
  stage_submit:{type:Sequelize.TINYINT,allowNull:false,defaultValue:1},
  added_by:{type:Sequelize.INTEGER,allowNull:false,defaultValue:null},
  timestamp:{type:Sequelize.INTEGER,allowNull:false,defaultValue:null},
  updated_on:{type:Sequelize.INTEGER,allowNull:false,defaultValue:0},
  approved_on:{type:Sequelize.INTEGER,allowNull:true,defaultValue:null},
  approved_by:{type:Sequelize.INTEGER,allowNull:true,defaultValue:null},
  status:{type:Sequelize.TINYINT,allowNull:false,defaultValue:1},
},{timestamps:false})
const addPartnerTeam = async(data)=>{
  try {
    let results = await PartnerTeam.create(data)
    if (results) {
        return results;
    }
    else {
        return false;
    }
  } catch (error) {
    console.log(error)
  }
}
const addPartnerstage = async(data)=>{
  try {
    console.log("$$$$$$$$$$$$$$$$$",data);
    let results = await PartnerStage.create(data)
    if (results[0]) {
        return results[0].id;
    }
    else {
        return false;
    }
  } catch (error) {
    console.log(error)
  }
}

const updatePartnerStage = async (id, data) => {
    try {
      const updatedRows = await PartnerStage.update(data, {
        where: {
          id: id
        },
        returning: true,
      });
      if (updatedRows) {
        return true;
      } else {
        return false;
      }
    } catch (error) {
      console.error(error);
      return false;
    }
  };
  const addRfqStage = async(data)=>{
    try {
      console.log("$$$$$$$$$$$$$$$$$",data);
      let results = await RfqStage.create(data)
      if (results[0]) {
          return results[0].id;
      }
      else {
          return false;
      }
    } catch (error) {
      console.log(error)
    }
  }
  
  const updateRfqStage = async (id, data) => {
      try {
        const updatedRows = await RfqStage.update(data, {
          where: {
            id: id
          },
          returning: true,
        });
        if (updatedRows) {
          return true;
        } else {
          return false;
        }
      } catch (error) {
        console.error(error);
        return false;
      }
    };
  module.exports={
    updatePartnerStage,
    addPartnerstage,
    addPartnerTeam,
    addRfqStage,
    updateRfqStage
  }