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

const evictionConfigDB: any = {};

evictionConfigDB.create = async ({
  clientId,
  propId,
  lockInServe,
  clearDues,
  midCycleCharge,
  restrictMoveOut,
}: evictionConfigTypes) => {
  const query = `INSERT INTO EvictionConfig (clientId, propId, lockInServe, clearDues, midCycleCharge, restrictMoveOut) VALUES (?, ?, ?, ?, ?, ?)`;
  const data = [
    clientId,
    propId,
    lockInServe,
    clearDues,
    midCycleCharge,
    restrictMoveOut,
  ];
  const [rows] = await DB.query<RowDataPacket[]>(query, data);
  return true;
};

evictionConfigDB.update = async ({
  clientId,
  propId,
  lockInServe,
  clearDues,
  midCycleCharge,
  restrictMoveOut,
}: evictionConfigTypes) => {
  const query = `Update EvictionConfig set lockInServe = ?, clearDues = ?, midCycleCharge = ?, restrictMoveOut = ? where clientId = ? and propId = ?`;
  const data = [
    lockInServe,
    clearDues,
    midCycleCharge,
    restrictMoveOut,
    clientId,
    propId,
  ];
  const [rows] = await DB.query<RowDataPacket[]>(query, data);
  return true;
};

evictionConfigDB.getByClientIdAndPropId = async ({
  clientId,
  propId,
}: evictionConfigTypes) => {
  const query = `SELECT * FROM EvictionConfig 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 evictionConfigDB;
