import { ResultSetHeader, RowDataPacket } from "mysql2";
import DB from "../config/database/db";
import argeementRenewConfigTypes from "../schemas/argeementRenewConfig.scheme";

const argeementRenewConfigDB: any = {};

argeementRenewConfigDB.create = async ({
  clientId,
  propId,
  autoRenew,
  rentIncrementType,
  rentIncrementValue,
  updateSecurity,
  agreementCharges,
}: argeementRenewConfigTypes) => {
  const query = `INSERT INTO ArgeementRenewConfig (clientId, propId, autoRenew, rentIncrementType, rentIncrementValue, updateSecurity, agreementCharges) VALUES (?, ?, ?, ?, ?, ?, ?)`;
  const data = [
    clientId,
    propId,
    autoRenew,
    rentIncrementType,
    rentIncrementValue,
    updateSecurity,
    agreementCharges,
  ];
  const [rows] = await DB.query<RowDataPacket[]>(query, data);
  return true;
};

argeementRenewConfigDB.update = async ({
  clientId,
  propId,
  autoRenew,
  rentIncrementType,
  rentIncrementValue,
  updateSecurity,
  agreementCharges,
}: argeementRenewConfigTypes) => {
  const query = `Update ArgeementRenewConfig set autoRenew = ?, rentIncrementType = ?, rentIncrementValue = ?, updateSecurity = ?, agreementCharges = ? where clientId = ? and propId = ?`;
  const data = [
    autoRenew,
    rentIncrementType,
    rentIncrementValue,
    updateSecurity,
    agreementCharges,
    clientId,
    propId,
  ];
  const [rows] = await DB.query<RowDataPacket[]>(query, data);
  return true;
};

argeementRenewConfigDB.getByClientIdAndPropId = async ({
  clientId,
  propId,
}: argeementRenewConfigTypes) => {
  const query = `SELECT * FROM ArgeementRenewConfig where clientId = ? and propId = ?`;
  const data = [clientId, propId];
  const [rows] = await DB.query<RowDataPacket[]>(query, data);
  if (rows && rows.length > 0) return rows[0];
  return false;
};

export default argeementRenewConfigDB;
