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

const staffDocumentDB: any = {};

staffDocumentDB.add = async ({
  staffId,
  clientId,
  type,
  value,
}: staffDocumentTypes) => {
  const query =
    "Insert into StaffDocuments (staffId, clientId, type, value) values (?,?,?,?)";
  const data = [staffId, clientId, type, value];
  const [rows] = await DB.query<ResultSetHeader>(query, data);
  return rows.insertId;
};

staffDocumentDB.updateDoc = async ({
  staffId,
  clientId,
  type,
  value,
  id,
}: staffDocumentTypes) => {
  const query =
    "Update StaffDocuments set staffId = ?, clientId = ?, type = ?, value = ? where id = ?";
  const data = [staffId, clientId, type, value, id];
  const [rows] = await DB.query<ResultSetHeader>(query, data);
  return rows.affectedRows;
};

staffDocumentDB.getByStaffId = async ({
  staffId,
  clientId,
}: staffDocumentTypes) => {
  const query =
    "Select * from StaffDocuments where staffId = ? and clientId = ?";
  const data = [staffId, clientId];
  const [rows] = await DB.execute<RowDataPacket[]>(query, data);
  if (rows?.length > 0) return rows;
  else return false;
};

staffDocumentDB.getByStaffIdAndType = async ({
  staffId,
  clientId,
  type,
}: staffDocumentTypes) => {
  const query =
    "Select * from StaffDocuments where staffId = ? and clientId = ? and type = ?";
  const data = [staffId, clientId, type];
  const [rows] = await DB.execute<RowDataPacket[]>(query, data);
  if (rows?.length > 0) return rows[0];
  else return false;
};

export default staffDocumentDB;
