import { ResultSetHeader, RowDataPacket } from "mysql2";
import DB from "../config/database/db";
import reportTypes from "../schemas/reports.schema";

const reportDB: any = {};

reportDB.add = async ({
  reportName,
  reportType,
  clientId,
  propId,
  status,
  value,
  generatedBy,
  startDate,
  endDate,
}: reportTypes) => {
  const query =
    "Insert into Reports (reportName, reportType, clientId, propId, status, value, generatedBy, startDate, endDate) values (?, ?, ?, ?, ?, ?, ?, ?, ?)";
  const data = [
    reportName,
    reportType,
    clientId,
    propId,
    status,
    value,
    generatedBy,
    startDate,
    endDate,
  ];
  const [rows] = await DB.query<ResultSetHeader>(query, data);
  return rows.insertId;
};

reportDB.getListByClientId = async ({ clientId, pageNum, limit }: reportTypes & {limit: any; pageNum: any;}) => {
  const query = "Select * from Reports where clientId = ? order by id desc limit ?, ?";
  const offset: number = (pageNum - 1) * limit;
  const data = [clientId, offset, limit];

  const [rows] = await DB.query<RowDataPacket[]>(query, data);
  return rows;
};

export default reportDB;
