import { RowDataPacket } from "mysql2";
import DB from "../../../config/database/db";
import CONSTANTS from "../../../config/constants";

const whatsappOccupancyDB: any = {};

whatsappOccupancyDB.moveInsByClientIdAndDate = async ({
  clientId,
  moveInDate
}: any) => {
  const query =
    "Select O.moveInDate, T.name as tenantName, T.mobile as tenantMobile, P.name as propName, R.roomNum, COALESCE(F.name, O.floor) as flatFloor from Occupancies as O join Tenants as T on T.id = O.tenantId join Rooms as R on R.id = O.roomId join Properties as P on P.id = O.propId left join Flats as F on F.id = O.flatId where O.clientId = ? and O.status = ? and DATE(O.moveInDate) = ?";
  const data = [clientId, CONSTANTS.OCCUPANCY_STATUS.OCCUPIED, moveInDate];
  const [rows] = await DB.execute<RowDataPacket[]>(query, data);
  if (rows?.length > 0) return rows;
  else return false;
};

whatsappOccupancyDB.moveOutsByClientIdAndDate = async ({
  clientId,
  moveOutDate
}: any) => {
  const query =
    "Select M.moveOutDate, T.name as tenantName, T.mobile as tenantMobile, P.name as propName, R.roomNum, COALESCE(F.name, M.floor) as flatFloor from MoveOut as M join Tenants as T on T.id = M.tenantId join Rooms as R on R.id = M.roomId join Properties as P on P.id = M.propId left join Flats as F on F.id = M.flatId where M.clientId = ? and M.status = ? and DATE(M.moveOutDate) = ?";
  const data = [clientId, CONSTANTS.MOVE_OUT_STATUS.MOVEOUT, moveOutDate];
  const [rows] = await DB.execute<RowDataPacket[]>(query, data);
  if (rows?.length > 0) return rows;
  else return false;
};

export default whatsappOccupancyDB;