const { INTEGER, STRING, TEXT, FLOAT, TINYINT, CHAR } = require("sequelize");
const sequelize = require("../config/dbconnection");
const { empty } = require("../Helpers/Common");
const { logOut } = require("../controllers/login");

const JobSchedule=sequelize.define('job_schedular',{
    id: { type: INTEGER, allowNull: false, primaryKey: true, autoIncrement: true },
    project_id: { type: INTEGER, defaultValue: null },
    task_id: { type: INTEGER, defaultValue: null },
    subtask_id: { type: INTEGER, defaultValue: null },
    shift_start: { type: INTEGER, defaultValue: null },
    shift_end: { type: INTEGER, defaultValue: null },
    team:{ type: INTEGER, defaultValue: null},
    status: { type: TINYINT, defaultValue: 1 },
    added_by: { type: INTEGER, defaultValue: null },
    timestamp: { type: INTEGER, allowNull: false },
  
  },{timestamps:false})


const addschedule=async(data)=>{
    let res=await JobSchedule.create(data)
    return res.id;
  }
  
const getschedule=async(userType,userId,hasViewPermission)=>{
    let query=""
    console.log("HIFIIIIIIIIIIIIIIIIII123333",userType);
    if(userType==1 || hasViewPermission){
        console.log("HIFIIIIIIIIIIIIIIIIII123333",userType);
    query=await sequelize.query(`select j.*,p.name as project_name,sm.first_name as staff,pt.name as task_name,ps.name as sub_task_name,p.short_code, sm.id AS staff_id  from job_schedulars j
    left join projects p on p.id=j.project_id
    left join project_tasks pt on pt.id=j.task_id 
    left join project_subtasks ps on ps.id=j.subtask_id
    left join staff_members sm on sm.id=j.team
    where p.id IS NOT NULL
    `)
    }
 else{
    query=await sequelize.query(`select j.*,p.name as project_name,pt.name as task_name,sm.first_name as staff,p.short_code from job_schedulars j
    left join projects p on p.id=j.project_id
    left join project_tasks pt on pt.id=j.task_id
    left join staff_members sm on sm.id=j.team
    left join users u on u.id=sm.user_id
    where u.id=${userId}  AND p.id IS NOT NULL  
    `)
    }
    console.log("HIFIIIIIIIIIIIIIIIIII123333");
    let data=[]
    if(query[0].length>0){
      data=query[0]
    }
    return data;
}
module.exports={addschedule,getschedule}