"use client";
import {withFormik} from "formik";
import InnerCreateTransferTeamForm from "./InnerCreateTransferTeamForm";
import {AppRouterInstance} from "next/dist/shared/lib/app-router-context.shared-runtime";
import {UsersType} from "@/constants";
import {UseMutateAsyncFunction} from "@tanstack/react-query";
import {toast} from "react-toastify";
import {handleAxiosError} from "@/lib/utils";
import { ServerResponse } from "@/types";

export interface CreateTransferTeamFormProps {
  router: AppRouterInstance;
  members: {
    translations: {
      id: number;
      lang_id: number | null;
      firstName: string | null;
      lastName: string | null;
      position: string | null;
    }[];

    id: number;
    slug: string;
    type: UsersType;
    phone: string | null;
    email: string | null;
    teamName: string | null;
    medicalNumber: string | null;
    expertiseId: number | null;
    imageId: number | null;
    createdAt: Date;
  }[];
  packages: [];
  fetchMembersLoading: boolean;
  fetchPackagesLoading: boolean;
  createFn: UseMutateAsyncFunction<ServerResponse, Error, CreateTransferTeamFormValues, ServerResponse>;
  createPending: boolean;
}

export interface CreateTransferTeamFormValues {
  name: string;
  members: {id: string | null; name: string; index: string}[];
  introduction: string;
  services: string;
  duties: string;
  packages: [];
}
const CreateTransferTeamForm = withFormik<
  CreateTransferTeamFormProps,
  CreateTransferTeamFormValues
>({
  mapPropsToValues: () => ({
    name: "",
    introduction: "",
    services: "",
    duties: "",
    packages: [],
    members: [],
  }),
  handleSubmit: async (values, {props}) => {
    try {
      const {message} = await props.createFn(values);
      toast.success(message);
    } catch (error) {
      toast.error(handleAxiosError(error));
    }
  },
})(InnerCreateTransferTeamForm);

export default CreateTransferTeamForm;
