import {withFormik} from "formik";
import {AppRouterInstance} from "next/dist/shared/lib/app-router-context.shared-runtime";

import {getSinglePatientDataType, ServerResponse} from "@/types";
import {toast} from "react-toastify";
import {UseMutateAsyncFunction} from "@tanstack/react-query";
import InnerUpdatePatientForm from "./InnerUpdatePatientForm";

export interface CreatePatientFormValues {
  firstName: string;
  lastName: string;
  nationality: string | null;
  nationalityCode: string;
  passportCode: string;
  age?: number | null;
  sex?: "male" | "female" | "other";
  birthDate?: string | Date;
  address?: string;
  phone?: string;
  email?: string;
  preferredLanguage?: string;
  postalCode?: string;
}

export type UpdatePatientVariables = {
  data: CreatePatientFormValues;
  id: string;
};
export interface UpdatePatientFormProps {
  router: AppRouterInstance;
  preValues: getSinglePatientDataType;
  countries?: {
    name: string;
    callCode: string;
    id: number;
    coverId: number | null;
    createdAt: Date;
  }[];
  fetchCountriesLoading:boolean,
  updateFn: UseMutateAsyncFunction<
    {status: number; data?: getSinglePatientDataType[]; message: string},
    Error,
    UpdatePatientVariables,
    ServerResponse
  >;
  id: string;
}

const UpdatePatientForm = withFormik<
  UpdatePatientFormProps,
  CreatePatientFormValues
>({
  mapPropsToValues: (props) => ({
    firstName: props?.preValues?.firstName  || "",
    lastName: props?.preValues?.lastName  || "",
    nationality: props?.preValues?.nationalityId  ? String(props?.preValues?.nationalityId) : null,
    nationalityCode: props?.preValues?.nationalityCode  || "",
    passportCode: props?.preValues?.passportCode  || "",
    postalCode: props?.preValues?.postalCode || "",
    sex: props?.preValues?.sex || "male",
    address: props?.preValues?.address || "",
    birthDate: props?.preValues?.birthDate || "",
    email: props?.preValues?.email || "",
    phone: props?.preValues?.phone || "",
    preferredLanguage: "en",
  }),
  handleSubmit: async (values, {props}) => {
    try {

        console.log(props.id)
      const {message} = await props.updateFn({id: props.id,data: values, });
      toast.success(message);
    } catch{
      toast.error("dasd");
    }
  },
})(InnerUpdatePatientForm);

export default UpdatePatientForm;
