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

const landlordDocumentDB: any = {};

landlordDocumentDB.addDoc = async ({
  landlordId,
  clientId,
  propId,
  flatId=null,
  type,
  value,
}: landlordDocumentsTypes) => {
  const query =
    "Insert into LandlordDocuments (landlordId, clientId, propId, flatId, type, value) values (?,?,?,?,?,?)";
  const data = [landlordId, clientId, propId, flatId, type, value];
  const [rows] = await DB.query<ResultSetHeader>(query, data);
  return rows.insertId;
};

landlordDocumentDB.updateDoc = async ({
  landlordId,
  clientId,
  propId,
  flatId,
  type,
  value,
  id,
}: landlordDocumentsTypes) => {
  const query =
    "Update LandlordDocuments set landlordId = ?, clientId = ?, propId = ?, flatId = ?, type = ?, value = ? where id = ?";
  const data = [landlordId, clientId, propId, flatId, type, value, id];
  const [rows] = await DB.query<ResultSetHeader>(query, data);
  return rows.insertId;
};

landlordDocumentDB.getIDByType = async ({
  landlordId,
  clientId,
  type,
}: landlordDocumentsTypes) => {
  const query =
    "Select * from LandlordDocuments where landlordId = ? and clientId =? and type = ?";
  const data = [landlordId, clientId, type];
  const [rows] = await DB.execute<RowDataPacket[]>(query, data);
  if (rows?.length > 0) return rows[0];
  else return false;
};

landlordDocumentDB.getIDByClientIdAndPropIdAndFlatIdAndType = async ({
  landlordId,
  clientId,
  type,
  propId,
  flatId,
}: landlordDocumentsTypes) => {
  const query =
    "Select * from LandlordDocuments where landlordId = ? and clientId =? and type = ? and propId = ? and flatId = ?";
  const data = [landlordId, clientId, type, propId, flatId];
  const [rows] = await DB.execute<RowDataPacket[]>(query, data);
  if (rows?.length > 0) return rows[0];
  else return false;
};

landlordDocumentDB.getByClientIdAndLandlordIdAndPropId = async ({ clientId, landlordId, propId }: landlordDocumentsTypes) => {
  const query = "Select * from LandlordDocuments where clientId = ? and landlordId = ? and propId = ?";
  const data = [clientId, landlordId, propId];
  const [rows] = await DB.execute<RowDataPacket[]>(query, data);
  if (rows?.length > 0) return rows;
  else return false;
};

landlordDocumentDB.getByClientIdAndLandlordIdAndPropIdAndFlatId = async ({ clientId, landlordId, propId, flatId }: landlordDocumentsTypes) => {
  const query = "Select * from LandlordDocuments where clientId = ? and landlordId = ? and propId = ? and flatId = ?";
  const data = [clientId, landlordId, propId, flatId];
  const [rows] = await DB.execute<RowDataPacket[]>(query, data);
  if (rows?.length > 0) return rows;
  else return false;
};

landlordDocumentDB.getByClientIdAndLandlordId = async ({ clientId, landlordId }: landlordDocumentsTypes) => {
  const query = "Select * from LandlordDocuments where clientId = ? and landlordId = ?";
  const data = [clientId, landlordId];
  const [rows] = await DB.execute<RowDataPacket[]>(query, data);
  if (rows?.length > 0) return rows;
  else return false;
};

export default landlordDocumentDB;