import { apiClient } from "./client";
import { API_ENDPOINTS } from "../config/api";
import { getErrorMessage } from "../utils/error-helpers";
import log from "../utils/logger";

export interface FitnessProfile {
  id?: string;
  clientId: string;
  height?: number;
  weight?: number;
  goals?: string;
  fitnessLevel: "beginner" | "intermediate" | "advanced";
  medicalConditions?: string;
  dietaryRestrictions?: string;
  preferredWorkoutTime?: "morning" | "afternoon" | "evening";
  workoutFrequency?: number;
}

export const fitnessProfileApi = {
  getFitnessProfile: async (
    userId: string,
    token: string,
  ): Promise<FitnessProfile> => {
    try {
      log.debug("Getting fitness profile", { userId });
      const response = await apiClient.get(
        `${API_ENDPOINTS.USERS}/${userId}/fitness-profile`,
        {
          headers: {
            Authorization: `Bearer ${token}`,
          },
        },
      );
      return response.data;
    } catch (error: unknown) {
      log.error("Failed to fetch fitness profile", error);
      throw error;
    }
  },

  updateFitnessProfile: async (
    userId: string,
    data: Partial<FitnessProfile>,
    token: string,
  ): Promise<FitnessProfile> => {
    try {
      const response = await apiClient.put(
        `${API_ENDPOINTS.USERS}/${userId}/fitness-profile`,
        data,
        {
          headers: {
            Authorization: `Bearer ${token}`,
          },
        },
      );
      return response.data;
    } catch (error: unknown) {
      log.error("Failed to update fitness profile", error);
      throw error;
    }
  },

  createFitnessProfile: async (
    data: Omit<FitnessProfile, "id">,
    token: string,
  ): Promise<FitnessProfile> => {
    try {
      const response = await apiClient.post(
        `${API_ENDPOINTS.USERS}/${data.clientId}/fitness-profile`,
        data,
        {
          headers: {
            Authorization: `Bearer ${token}`,
          },
        },
      );
      return response.data;
    } catch (error: unknown) {
      log.error("Failed to create fitness profile", error);
      throw error;
    }
  },
};
