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

const tenantGuardianDB: any = {};

tenantGuardianDB.getByTenantId = async ({ tenantId }: any) => {
  const query = "Select fatherName, fatherMobile, fatherEmail, motherName, motherMobile, motherEmail, fatherOccupation, fatherAnnualIncome, localGuardianName, localGuardianMobile, localGuardianEmail, localGuardianRelation from TenantGuardians where tenantId = ?";
  const data = [tenantId];
  const [rows] = await DB.execute<RowDataPacket[]>(query, data);
  if (rows?.length > 0) return rows[0];
  else return false;
};

tenantGuardianDB.create = async ({
  tenantId,
  fatherName,
  fatherMobile,
  fatherEmail=null,
  fatherOccupation,
  fatherAnnualIncome,
  motherName,
  motherMobile,
  motherEmail=null,
  localGuardianName,
  localGuardianMobile,
  localGuardianEmail=null,
  localGuardianRelation,
}: tenantGuardianTypes) => {
  const query =
    "INSERT INTO TenantGuardians (tenantId, fatherName, fatherMobile, fatherEmail, fatherOccupation, fatherAnnualIncome, motherName, motherMobile, motherEmail, localGuardianName, localGuardianMobile, localGuardianEmail, localGuardianRelation) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
  const data = [
    tenantId,
    fatherName,
    fatherMobile,
    fatherEmail,
    fatherOccupation,
    fatherAnnualIncome,
    motherName,
    motherMobile,
    motherEmail,
    localGuardianName,
    localGuardianMobile,
    localGuardianEmail,
    localGuardianRelation,
  ];
  const [rows] = await DB.query<ResultSetHeader>(query, data);
  return rows.insertId;
};

tenantGuardianDB.createNew = async ({
  tenantId,
  fatherMobile,
  motherName,
  motherMobile,
}: tenantGuardianTypes) => {
  const query =
    "INSERT INTO TenantGuardians (tenantId, fatherMobile, motherName, motherMobile) VALUES (?, ?, ?, ?)";
  const data = [
    tenantId,
    fatherMobile,
    motherName,
    motherMobile,
  ];
  const [rows] = await DB.query<ResultSetHeader>(query, data);
  return rows.insertId;
};

tenantGuardianDB.update = async ({
  tenantId,
  fatherName,
  fatherMobile,
  fatherEmail,
  fatherOccupation,
  fatherAnnualIncome,
  motherName,
  motherMobile,
  motherEmail,
  localGuardianName,
  localGuardianMobile,
  localGuardianEmail,
  localGuardianRelation,
}: tenantGuardianTypes) => {
  const query =
    "UPDATE TenantGuardians SET fatherName = ?, fatherMobile = ?, fatherEmail = ?, fatherOccupation = ?, fatherAnnualIncome = ?, motherName = ?, motherMobile = ?, motherEmail = ?, localGuardianName = ?, localGuardianMobile = ?, localGuardianEmail = ?, localGuardianRelation = ? WHERE tenantId = ?";
  const data = [
    fatherName,
    fatherMobile,
    fatherEmail,
    fatherOccupation,
    fatherAnnualIncome,
    motherName,
    motherMobile,
    motherEmail,
    localGuardianName,
    localGuardianMobile,
    localGuardianEmail,
    localGuardianRelation,
    tenantId,
  ];
  const [rows] = await DB.query<ResultSetHeader>(query, data);
  return rows.insertId;
};

tenantGuardianDB.updateNew = async ({
  tenantId,
  fatherMobile,
  motherName,
  motherMobile,
}: tenantGuardianTypes) => {
  const query =
    "UPDATE TenantGuardians SET  fatherMobile = ?, motherName = ?, motherMobile = ? WHERE tenantId = ?";
  const data = [
    fatherMobile,
    motherName,
    motherMobile,
    tenantId,
  ];
  const [rows] = await DB.query<ResultSetHeader>(query, data);
  return rows.insertId;
};

tenantGuardianDB.updateFatherName = async ({
  tenantId,
  fatherName
}: tenantGuardianTypes) => {
  const query =
    "UPDATE TenantGuardians SET fatherName = ? WHERE tenantId = ?";
  const data = [
    fatherName,
    tenantId,
  ];
  const [rows] = await DB.query<ResultSetHeader>(query, data);
  return rows.insertId;
};

tenantGuardianDB.addFatherName = async ({
  tenantId,
  fatherName
}: tenantGuardianTypes) => {
  const query =
    "INSERT INTO TenantGuardians (tenantId, fatherName) VALUES (?, ?)";
  const data = [
    tenantId,
    fatherName
  ];
  const [rows] = await DB.query<ResultSetHeader>(query, data);
  return rows.insertId;
};

tenantGuardianDB.updateFatherMobile = async ({
  tenantId,
  fatherMobile
}: tenantGuardianTypes) => {
  const query =
    "UPDATE TenantGuardians SET fatherMobile = ? WHERE tenantId = ?";
  const data = [
    fatherMobile,
    tenantId,
  ];
  const [rows] = await DB.query<ResultSetHeader>(query, data);
  return rows.insertId;
};

tenantGuardianDB.addFatherMobile = async ({
  tenantId,
  fatherMobile
}: tenantGuardianTypes) => {
  const query =
    "INSERT INTO TenantGuardians (tenantId, fatherMobile) VALUES (?, ?)";
  const data = [
    tenantId,
    fatherMobile
  ];
  const [rows] = await DB.query<ResultSetHeader>(query, data);
  return rows.insertId;
};

export default tenantGuardianDB;
