import { withFormik } from "formik";
import { AppRouterInstance } from "next/dist/shared/lib/app-router-context.shared-runtime";
import InnerUpdateDoctorForm from "./InnerUpdateDoctorForm";
import { CreateUserTranslation, Expertise, getSingleUserDataType, Language } from "@/types";
import { toast } from "react-toastify";
import { UseMutateAsyncFunction } from "@tanstack/react-query";
import { UsersType } from "@/constants";

export interface CreateDoctorFormValues {
  translations: CreateUserTranslation[];
  type: UsersType;
  email: string;
  phone: string;
  medicalNumber: string;
  imageId: number | null;
  expertiseId: number | null;
}

export type UpdateUserVariables = {
  data: CreateDoctorFormValues;
  id: string;
};
export interface UpdateDoctorFormProps {
  router: AppRouterInstance;
  preValues: getSingleUserDataType;
  expertises: Expertise[];
  languages: Language[];
  updateFn: UseMutateAsyncFunction<
    { status: number; data?: getSingleUserDataType[]; message: string },
    unknown,
    UpdateUserVariables,
    unknown
  >;
  id: string;
}

const UpdateDoctorForm = withFormik<
  UpdateDoctorFormProps,
  CreateDoctorFormValues
>({
  mapPropsToValues: (props) => ({
    translations: props?.preValues?.translations,
    email: props?.preValues?.email,
    phone: props?.preValues?.phone,
    expertiseId: props.preValues.expertiseId,
    imageId: null,
    medicalNumber: props?.preValues?.medicalNumber ?? 0,
    type: "DOCTOR",
  }),
  handleSubmit: async (values, { props }) => {
    try {
      const { message } = await props.updateFn({ data: values, id: props.id });
      toast.success(message);
    } catch (error) {
      toast.error("dasd");
    }
  },
})(InnerUpdateDoctorForm);

export default UpdateDoctorForm;
