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

const eqaroTenantsDB: any = {};

eqaroTenantsDB.create = async ({
  tenantId,
  propId,
  eqaroUserId,
  eqaroProfileId,
  status,
  occupation,
  mobile,
  monthlyIncome,
  accessToken,
  refreshToken,
  accessTokenExpires,
  refreshTokenExpires,
}: eqaroTenantsType) => {
  const query =
    "Insert into EqaroTenants (tenantId, propId, eqaroUserId, eqaroProfileId, status, occupation, mobile, monthlyIncome, accessToken, refreshToken, accessTokenExpires, refreshTokenExpires) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
  const data = [
    tenantId,
    propId,
    eqaroUserId,
    eqaroProfileId,
    status,
    occupation,
    mobile,
    monthlyIncome,
    accessToken,
    refreshToken,
    accessTokenExpires,
    refreshTokenExpires,
  ];
  const [rows] = await DB.query<ResultSetHeader>(query, data);
  return rows.insertId;
};

eqaroTenantsDB.getByTenantId = async ({ tenantId }: eqaroTenantsType) => {
  const query = "Select * from EqaroTenants where tenantId = ?";
  const data = [tenantId];
  const [rows] = await DB.query<RowDataPacket[]>(query, data);
  return rows[0];
};

eqaroTenantsDB.updateStatus = async ({
  tenantId,
  status,
}: eqaroTenantsType) => {
  const query = "Update EqaroTenants set status = ? where tenantId = ?";
  const data = [status, tenantId];
  const [rows] = await DB.query<ResultSetHeader>(query, data);
  return rows.affectedRows;
};

eqaroTenantsDB.updateTenant = async ({
  tenantId,
  monthlyIncome,
  occupation,
  accessToken,
  refreshToken,
  accessTokenExpires,
  refreshTokenExpires,
  eqaroUserId,
  eqaroProfileId,
}: eqaroTenantsType) => {
  const query =
    "Update EqaroTenants set monthlyIncome = ?, occupation = ?, accessToken = ?, refreshToken = ?, accessTokenExpires = ?, refreshTokenExpires = ?, eqaroUserId = ?, eqaroProfileId = ? where tenantId = ?";
  const data = [
    monthlyIncome,
    occupation,
    accessToken,
    refreshToken,
    accessTokenExpires,
    refreshTokenExpires,
    eqaroUserId,
    eqaroProfileId,
    tenantId,
  ];
  const [rows] = await DB.query<ResultSetHeader>(query, data);
  return rows.affectedRows;
};

eqaroTenantsDB.updateTenantAccessToken = async ({
  tenantId,
  accessToken,
  accessTokenExpires,
}: eqaroTenantsType) => {
  const query =
    "Update EqaroTenants set accessToken = ?, accessTokenExpires = ? where tenantId = ?";
  const data = [accessToken, accessTokenExpires, tenantId];
  const [rows] = await DB.query<ResultSetHeader>(query, data);
  return rows.affectedRows;
};

eqaroTenantsDB.updateInitiateBondParams = async ({
  tenantId,
  orderId,
  bondFee,
  bondEffectiveDate,
  bondAmount,
}: eqaroTenantsType) => {
  const query =
    "Update EqaroTenants set orderId = ?, bondFee = ?, bondEffectiveDate = ?, bondAmount=? where tenantId = ?";
  const data = [orderId, bondFee, bondEffectiveDate, bondAmount, tenantId];
  const [rows] = await DB.query<ResultSetHeader>(query, data);
  return rows.affectedRows;
};

eqaroTenantsDB.updateCompleteBondParams = async ({
  tenantId,
  bondId,
  bondExpiryDate,
}: eqaroTenantsType) => {
  const query =
    "Update EqaroTenants set bondId = ?, bondExpiryDate = ? where tenantId = ?";
  const data = [bondId, bondExpiryDate, tenantId];
  const [rows] = await DB.query<ResultSetHeader>(query, data);
  return rows.affectedRows;
};

eqaroTenantsDB.updateCoApplicantId = async ({
  tenantId,
  coApplicantId,
}: eqaroTenantsType) => {
  const query = "Update EqaroTenants set coApplicantId = ? where tenantId = ?";
  const data = [coApplicantId, tenantId];
  const [rows] = await DB.query<ResultSetHeader>(query, data);
  return rows.affectedRows;
};

eqaroTenantsDB.updateBondPdf = async ({
  tenantId,
  bondUrl,
}: eqaroTenantsType) => {
  const query = "Update EqaroTenants set bondUrl = ? where tenantId = ?";
  const data = [bondUrl, tenantId];
  const [rows] = await DB.query<ResultSetHeader>(query, data);
  return rows.affectedRows;
};

eqaroTenantsDB.getCoApplicantDetails = async ({
  tenantId,
}: eqaroTenantsType) => {
  const query = "Select * from EqaroCoApplicants where tenantId = ? limit 1";
  const data = [tenantId];
  const [rows] = await DB.query<RowDataPacket[]>(query, data);
  return rows[0];
};

eqaroTenantsDB.getTenantDetails = async ({ tenantId }: eqaroTenantsType) => {
  const query =
    "Select et.occupation, et.mobile, et.bondExpiryDate, et.bondEffectiveDate, et.bondFee, et.monthlyIncome, et.bondUrl, t.name, t.gender, t.address, t.dob, t.email from EqaroTenants as et join Tenants as t on et.tenantId = t.id where tenantId = ? limit 1";
  const data = [tenantId];
  const [rows] = await DB.query<RowDataPacket[]>(query, data);
  return rows[0];
};

eqaroTenantsDB.deleteTenant = async ({ tenantId }: eqaroTenantsType) => {
  const query = "Delete from EqaroTenants where tenantId = ? limit 1";
  const data = [tenantId];
  const [rows] = await DB.query<ResultSetHeader>(query, data);
  return rows.affectedRows;
};

eqaroTenantsDB.updateBondDetails = async ({
  tenantId,
  bondAmount,
  bondFee,
  bondId,
  bondExpiryDate,
  bondEffectiveDate,
  orderId,
}: eqaroTenantsType) => {
  const query =
    "Update EqaroTenants set bondAmount = ?, bondFee = ?, bondId = ?, bondExpiryDate = ?, bondEffectiveDate = ?, orderId = ? where tenantId = ?";
  const data = [
    bondAmount,
    bondFee,
    bondId,
    bondExpiryDate,
    bondEffectiveDate,
    orderId,
    tenantId,
  ];
  const [rows] = await DB.query<ResultSetHeader>(query, data);
  return rows.affectedRows;
};

export default eqaroTenantsDB;
