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

const notificationPrefDB: any = {};

notificationPrefDB.getById = async ({
  id,
  notificationType,
}: notificationPrefTypes) => {
  const query =
    "Select * from NotificationPreferences where clientId=? and notificationType=?";
  const data = [id, notificationType];
  const [rows] = await DB.execute<RowDataPacket[]>(query, data);
  if (rows?.length > 0) return rows[0];
  else return false;
};

notificationPrefDB.add = async ({
  clientId,
  propId,
  notificationType,
  isEnabled,
  isSelfBranding,
}: notificationPrefTypes) => {
  const query =
    "Insert into NotificationPreferences (clientId, notificationType, isEnabled, isSelfBranding, propId) values (?, ?, ?, ?, ?)";
  const data = [clientId, notificationType, isEnabled, isSelfBranding, propId];
  await DB.execute<ResultSetHeader[]>(query, data);
  return true;
};

notificationPrefDB.enableDisableNotification = async ({
  clientId,
  notificationType,
  isEnabled,
}: notificationPrefTypes) => {
  const query =
    "Update NotificationPreferences set isEnabled = ? where clientId = ? and notificationType = ?";
  const data = [isEnabled, clientId, notificationType];
  await DB.execute<ResultSetHeader[]>(query, data);
  return true;
};

export default notificationPrefDB;
