import { ReferenceResponseType } from "@/type/profileType";
import {
  ContactInformationTypeForDisplay,
  PersonalDetailsForDisplay,
  ProfileDataType,
  UserDetailsTypeForInitialData,
} from "@/type/userType";
import { OthersDialogKey } from "./hooks/useOthersSection";

export const mapProfileToUserDetails = (
  profile?: ProfileDataType,
): UserDetailsTypeForInitialData => {
  if (!profile) {
    return {
      personal_details: {} as PersonalDetailsForDisplay,
      contact_information: {} as ContactInformationTypeForDisplay,
      siblings: [],
    };
  }
  console.log("profile.gender---------", profile);
  console.log("profile.phone---------", profile.phone);

  return {
    personal_details: {
      full_name: profile.user?.name || "", // ❗ not in API → keep empty or handle separately
      profession_designation: profile.profession?.name || "",
      gender: profile.gender || "",
      date_of_birth: profile.dob || "",

      height: profile.height || "",

      blood_group: profile.blood_group || "",
      marital_status: profile.marital_status || "",
      religion: profile.religion || "",

      mobile_number: profile.user?.phone || "",
      secondary_mobile_number: profile.secondary_phone || "",

      email: profile.user?.email || "", // ❗ not in API

      passport_id: profile.passport_id || "",
      national_id: profile.national_id || "",
      birth_certificate_id: profile.birth_certificate_id || "",

      experience: profile.years_of_experience || "",

      address: profile.present_address || "",
      isVerify: false, // ❗ not from API
    },

    contact_information: {
      father_name: profile.father_name || "",
      mother_name: profile.mother_name || "",

      country: profile.country || "",
      city: profile.city || "",
      postal_code: profile.postal_code || "",

      present_address: profile.present_address || "",
      permanent_address: profile.permanent_address || "",

      emergency_contact: {
        phone: profile.emergency_contact || "",
        relation: profile.emergency_relation || "",
      },
    },

    siblings: [],
  };
};
export const mapReferenceToUserDetails = (
  profile?: ProfileDataType,
): UserDetailsTypeForInitialData => {
  return {
    reference:
      profile?.references?.map((ref) => ({
        id: ref.id,
        uuid: ref.uuid,

        name: ref.name,
        label: ref.name, // ✅ required by your type

        occupation: ref.occupation,
        organization: ref.organization,

        phone: ref.phone,
        mobile: ref.phone, // ✅ map API phone → mobile

        relationship: ref.relationship,

        profile_id: ref.profile_id,
        user_id: ref.user_id,

        verify: ref.verify ?? false, // ✅ API mismatch fix
      })) || [],
  };
};

///////////////////

export const buildPayload = (key: OthersDialogKey | "", data: any) => {
  switch (key) {
    case "references": {
      const payload = {};
      return payload;
    }
    case "interest": {
      const payload = {
        hobby_uuids:
          data[key]
            ?.map((row: any) => row.name?.id ?? row.name?.uuid)
            .filter(Boolean) || [],
      };
      return payload;
    }
    case "declaration": {
      const payload = data[key]?.[0];
      return payload;
    }

    case "personalValues": {
      const rows = data[key] ?? [];
      return {
        personal_value_uuids: rows
          .map((row: any) => row.name?.id ?? row.name?.uuid)
          .filter(Boolean),
      };
    }

    default: {
      return data[key]?.[0];
    }
  }
};
