 
import UserOnlineTime from "../../models/UserOnlineTime"; 
import { Op, Sequelize } from "sequelize"; 

interface Request {
    userId?: string | number; 
    status?: string;
    date: string; 
  }
 

const ListUserOnlineOffline = async ({date, userId, status}: Request): Promise<UserOnlineTime[]> => {

    let where_clause = {}

    
    if(userId && status){ 
      where_clause = {      
        [Op.and]: [ 
            {userId: userId},
            {status: status},
            { 
                "$createdAt$": Sequelize.where(Sequelize.fn("date", Sequelize.col("createdAt")), `${date}`)
            }, 
          ] 
      }
    }
    else if(userId){ 
        where_clause = {      
          [Op.and]: [ 
              {userId: userId}, 
              { 
                  "$createdAt$": Sequelize.where(Sequelize.fn("date", Sequelize.col("createdAt")), `${date}`)
              }, 
            ] 
        }
    }
    else if(status){  
        where_clause = {      
          [Op.and]: [ 
            {status: status},
              { 
                  "$createdAt$": Sequelize.where(Sequelize.fn("date", Sequelize.col("createdAt")), `${date}`)
              }, 
            ] 
        }
    }
    else{
        where_clause = {      
            [Op.and]: [  
                { 
                    "$createdAt$": Sequelize.where(Sequelize.fn("date", Sequelize.col("createdAt")), `${date}`)
                }, 
              ] 
          }
    } 
  
  const users = await UserOnlineTime.findAll({ 
    where: where_clause,
    raw:true, 
 
    attributes: ['id', 'userId', 'status',
    [Sequelize.fn("DATE_FORMAT",Sequelize.col("UserOnlineTime.onlineTime"),"%Y-%m-%d %H:%i:%s"),"onlineTime"],
    [Sequelize.fn("DATE_FORMAT",Sequelize.col("UserOnlineTime.createdAt"),"%Y-%m-%d %H:%i:%s"),"createdAt"], 
    [Sequelize.fn("DATE_FORMAT",Sequelize.col("UserOnlineTime.updatedAt"),"%Y-%m-%d %H:%i:%s"),"updatedAt"]],

    order: [["id", "ASC"]], 
  })  
 

  return users;
};

export default ListUserOnlineOffline;




 