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

const tallyConfigDB: any = {};

tallyConfigDB.getByClientId = async ({ clientId }: { clientId: number }) => {
  const query = `
    SELECT * FROM TallyConfig WHERE clientId = ?
  `;
  const [rows] = await DB.execute<RowDataPacket[]>(query, [clientId]);
  return rows[0];
};

tallyConfigDB.update = async ({ clientId, ip, port, billRefPrefix }: { clientId: number, ip: string; port: string; billRefPrefix: string; }) => {
  const query = `
    UPDATE TallyConfig SET ip = ?, port = ?, billRefPrefix = ? WHERE clientId = ?`;

  const data = [
    ip,
    port,
    billRefPrefix,
    clientId,
  ];

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

export default tallyConfigDB;
  