import * as functions from "firebase-functions";
import {
  DoctorDefaultBasePrice,
  DefaultDoctorAccountStatus,
} from "./constants";
import { deleteDoctor } from "./user-service";

/** delete doctor data function, this function will delete the doctor data, also with its user, if you only wanto delete the doctor data 
goto the user service
 */
export const deleteDoctorAccount = functions.https.onCall(
  async (request, response) => {
    try {
      console.log("delete doctor functions : " + request.doctorId);
      await deleteDoctor(request.doctorId);
    } catch (e) {
      throw e;
    }
  }
);
/** this function will detect the newly created doctor account and just change his balance to 0, we put it in cloud function
so they can't change their balance in client side for security reason*/
export const doctorAdded = functions.firestore
  .document("/Doctors/{doctorId}")
  .onCreate((snapshot, context) => {
    snapshot.ref.update({
      balance: 0,
      accountStatus: DefaultDoctorAccountStatus,
      doctorBasePrice: DoctorDefaultBasePrice,
    });
    return Promise.resolve();
  });
