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

const settingsDB: any = {};

settingsDB.getByClientIdAndPropId = async ({
  clientId,
  propId,
}: notificationPrefTypes) => {
  const query =
    "Select whatsApp, sms, logo, rentReminder, onboardWelcome, partialPayment, onlinePayment, footer, whatsappOnboardingTemplate, markPaidNotification, moveOutNotification, android, ios, brandName, markPaidTenantNotification from Settings where clientId=? and propId=?";
  const data = [clientId, propId];
  const [rows] = await DB.execute<RowDataPacket[]>(query, data);
  if (rows?.length > 0) return rows[0];
  else return false;
};

settingsDB.getByClientIdAndMultiplePropIds = async ({
  clientId,
  propIds,
}: notificationPrefTypes & {propIds: any}) => {
  const query =
    `Select whatsApp, sms, logo, rentReminder, onboardWelcome, partialPayment, onlinePayment, footer, whatsappOnboardingTemplate, markPaidNotification, moveOutNotification, android, ios from Settings where clientId=? and propId in (${propIds.map(() => '?').join(',')})`;
  const data = [clientId, ...propIds];
  const [rows] = await DB.execute<RowDataPacket[]>(query, data);
  if (rows?.length > 0) return rows;
  else return false;
};

settingsDB.getByClientId = async ({
  clientId,
}: notificationPrefTypes) => {
  const query =
    "Select whatsApp, sms, logo, rentReminder, onboardWelcome, partialPayment, onlinePayment, footer, whatsappOnboardingTemplate, markPaidNotification, moveOutNotification, android, ios from Settings where clientId=?";
  const data = [clientId];
  const [rows] = await DB.execute<RowDataPacket[]>(query, data);
  if (rows?.length > 0) return rows;
  else return false;
};

settingsDB.getEnabledTenantOnboard = async ({
  clientId,
}: notificationPrefTypes) => {
  const query =
    "Select * from Settings where clientId = ? and onboardWelcome = ?";
  const data = [clientId, 1];
  const [rows] = await DB.execute<RowDataPacket[]>(query, data);
  if (rows?.length > 0) return rows;
  else return false;
};

settingsDB.add = async ({
  clientId,
  propId,
}: notificationPrefTypes) => {
  const query =
    "Insert into Settings (clientId, propId, footer) values (?, ?, 'The Kipinn Team')";
  const data = [clientId, propId];
  //await DB.execute<ResultSetHeader[]>(query, data);
  const [rows] = await DB.query<ResultSetHeader>(query, data);
  return rows.insertId;
  //return true;
};

settingsDB.edit = async ({
  clientId,
  propIds,
  isEnabled,
  type
}: notificationPrefTypes & {type: string, propIds: any}) => {
  let query = '';
  if (type === 'whatsApp') {
    query =
      `Update Settings set whatsApp = ? where clientId = ? and propId in (${propIds.map(() => '?').join(',')})`;
  } else if (type === 'sms') {
    query =
      `Update Settings set sms = ? where clientId = ? and propId in (${propIds.map(() => '?').join(',')})`;
  } else if (type === 'rentReminder') {
    query =
      `Update Settings set rentReminder = ? where clientId = ? and propId in (${propIds.map(() => '?').join(',')})`;
  } else
    query =
      `Update Settings set onboardWelcome = ? where clientId = ? and propId in (${propIds.map(() => '?').join(',')})`;
  const data = [isEnabled, clientId, ...propIds];
  await DB.execute<ResultSetHeader[]>(query, data);
  return true;
};

settingsDB.editX = async ({
  clientId,
  propIds,
  whatsApp,
  sms,
  paymentReminders,
  onboardingWelcome,
}: notificationPrefTypes & {whatsApp: any, sms: any, paymentReminders: any, onboardingWelcome: any, propIds: any}) => {
  let query = `Update Settings set whatsApp = ?, sms = ?, rentReminder = ?, onboardWelcome = ? where clientId = ? and propId in (${propIds.map(() => '?').join(',')})`;
  const data = [whatsApp, sms, paymentReminders, onboardingWelcome, clientId, ...propIds];
  await DB.execute<ResultSetHeader[]>(query, data);
  return true;
};

settingsDB.editForClient = async ({
  clientId,
  isEnabled,
  type
}: notificationPrefTypes & {type: string}) => {
  let query = '';
  if (type === 'whatsApp') {
    query =
      "Update Settings set whatsApp = ? where clientId = ?";
  } else if (type === 'sms') {
    query =
      "Update Settings set sms = ? where clientId = ?";
  } else if (type === 'rentReminder') {
    query =
      "Update Settings set rentReminder = ? where clientId = ?";
  } else
    query =
      "Update Settings set onboardWelcome = ? where clientId = ?";
  const data = [isEnabled, clientId];
  await DB.execute<ResultSetHeader[]>(query, data);
  return true;
};


settingsDB.updateDefaultSettings = async ({
  id,
  onboardingTemplate,
  footer,
  android, 
  ios,
}: notificationPrefTypes & {onboardingTemplate: String; footer: String;  android: String; ios: String}) => {
  const query =
    " UPDATE Settings set whatsappOnboardingTemplate =?, footer=?, android=?, ios=? where id=? limit 1";
  const data = [onboardingTemplate, footer, android, ios, id];
  //await DB.execute<ResultSetHeader[]>(query, data);
  const [rows] = await DB.query<ResultSetHeader>(query, data);
  return rows.insertId;
  //return true;
};

settingsDB.updateTenantOnboard = async ({
  id,
  onboardWelcome,
}: notificationPrefTypes & {onboardWelcome: any}) => {
  const query =
    " UPDATE Settings set onboardWelcome = ? where id=? limit 1";
  const data = [onboardWelcome, id];
  //await DB.execute<ResultSetHeader[]>(query, data);
  const [rows] = await DB.query<ResultSetHeader>(query, data);
  return rows.insertId;
  //return true;
};

export default settingsDB;
