import { baseApiRequest } from "../apiRequests/baseApiRequest";
import { FxRateResponse } from "./type";

// interface FxRateApiResponse {
//   id: string;
//   year: string;
//   currency: string;
//   rate: string;
// }

// interface PaginatedResponse<T> {
//   content: T[];
// }

const PROJECT_MANAGEMENT_BASE_URL = `${process.env.NEXT_PUBLIC_PROJECT_MANAGEMENT_API_URL}/api/v1`;

export const getAllFXRate = async (): Promise<FxRateResponse> => {
  return await baseApiRequest(
    `${PROJECT_MANAGEMENT_BASE_URL}/fx-rate`,
    {
      method: "GET",
      headers: { "Content-Type": "application/json" },
      credentials: "include",
    },
    {
      isAccessTokenRequird: true,
      revalidatePaths: ["/fx-rate"],
    },
  );
};

export const getFxRateByYear = async (
  year: string,
): Promise<FxRateResponse> => {
  return await baseApiRequest(
    `${PROJECT_MANAGEMENT_BASE_URL}/fx-rate/year=${year}`,
    {
      method: "GET",
      headers: {
        "Content-Type": "application/json",
      },
      credentials: "include",
    },
    {
      isAccessTokenRequird: true,
      revalidatePaths: [`/fx-rate/year=${year}`],
    },
  );
};

// export type FxRateMap = Map<string, Map<string, number>>;

// export async function createFxRateMap(): Promise<FxRateMap> {
//   const fxRateMap: FxRateMap = new Map();

//   try {
//     console.log("Fetching FX Rates for map...");

//     const response = await baseApiRequest(
//       "${process.env.SALES_CRM_API_URL}/fxrate",
//       {
//         method: "GET",
//         credentials: "include",
//       },
//     );

//     const paginatedData = response as PaginatedResponse<FxRateApiResponse>;

//     if (!paginatedData || !Array.isArray(paginatedData.content)) {
//       console.error(
//         "Invalid response structure received from FX Rate API:",
//         paginatedData,
//       );
//       throw new Error("Failed to fetch FX Rates: Invalid data format.");
//     }

//     const rates: FxRateApiResponse[] = paginatedData.content;
//     console.log(`Processing ${rates.length} FX rates...`);

//     rates.forEach((rateData) => {
//       const year = rateData.year;
//       const currency = rateData.currency.toUpperCase();
//       const rateValue = parseFloat(rateData.rate);

//       if (!year || !currency || isNaN(rateValue)) {
//         console.warn("Skipping invalid rate data:", rateData);
//         return;
//       }

//       if (!fxRateMap.has(year)) {
//         fxRateMap.set(year, new Map<string, number>());
//       }
//       const yearMap = fxRateMap.get(year)!;
//       yearMap.set(currency, rateValue);
//     });

//     console.log("FX Rate Map created successfully:", fxRateMap);
//     return fxRateMap;
//   } catch (error) {
//     console.error("Error creating FX Rate Map:", error);
//     let errorMessage = "An unknown error occurred";
//     if (error instanceof Error) {
//       errorMessage = error.message;
//     } else if (typeof error === "string") {
//       errorMessage = error;
//     }
//     throw new Error(`Failed to create FX Rate Map: ${errorMessage}`);
//   }
// }
