// import axios from "axios";
// import crypto from "crypto";

// interface FetchPageDataParams {
//   host?: string;
//   [key: string]: any;
// }

// interface ApiResponse {
//   success?: boolean;
//   data?: any;
//   message?: string;
//   status?: number;
//   [key: string]: any;
// }

// export async function fetchPageData(
//   { host, ...rh }: FetchPageDataParams,
//   pageSlug: string
// ): Promise<ApiResponse> {
//   const baseUrl = "https://cms.admin.tezcommerce.com/api/client";
//   const timeStamp = Date.now();

//   const key = "2994b43e78e9caf26cd3ca27475783da37919e4768898d41";
//   const secret =
//     "9583f140ec4fb8ca0ce82812fb06eae6c9e9b59a999d6fba728a92616ed15297";

//   // ✅ Create payload + signature
//   const body = { timestamp: timeStamp };
//   const payload = JSON.stringify(body);
//   const signature = crypto
//     .createHmac("sha256", secret)
//     .update(payload)
//     .digest("hex");

//   // ✅ Headers
//   const headers: Record<string, string> = {
//     ...rh,
//     "X-AUTH-APIKEY": key,
//     "X-AUTH-SIGNATURE": signature,
//     "X-AUTH-TIMESTAMP": timeStamp.toString(),
//     "Content-Type": "application/json",
//     "x-host": host || "localhost:3000",
//   };

//   console.log("==== DEBUG fetchPageData ====");
//   console.log("Page Slug:", pageSlug);
//   console.log("Final URL:", `${baseUrl}/page/fetch-single-page/${pageSlug}`);
//   console.log("Headers:", headers);
//   console.log("============================");

//   try {
//     const response = await axios.get<ApiResponse>(
//       `${baseUrl}/page/fetch-single-page/${pageSlug}`,
//       { headers }
//     );

//     console.log("✅ API Response:", response.data);
//     return response.data;
//   } catch (error: unknown) {
//     if (axios.isAxiosError(error)) {
//       console.error("❌ Axios Error:", {
//         message: error.message,
//         code: error.code,
//         status: error.response?.status,
//         statusText: error.response?.statusText,
//         data: error.response?.data,
//       });

//       return {
//         success: false,
//         message: error.message,
//         status: error.response?.status,
//         data: error.response?.data,
//       };
//     } else if (error instanceof Error) {
//       console.error("❌ Unexpected Error:", error.message);
//       return { success: false, message: error.message };
//     } else {
//       console.error("❌ Unknown Error:", error);
//       return { success: false, message: "Unknown error occurred" };
//     }
//   }
// }

import axios from "axios";
import crypto from "crypto";

interface FetchMenuDataParams {
  host?: string;
  [key: string]: any;
}

interface ApiResponse {
  success?: boolean;
  data?: any;
  [key: string]: any;
}

export async function fetchPageData(
  { host, ...rh }: FetchMenuDataParams,
  uid: string,
): Promise<ApiResponse> {
  const baseUrl = "https://cms.admin.tezcommerce.com/api/client";
  const timeStamp = Date.now();

  const key = "745f205f13127927d70bacae28042b47479199a98586140e";
  const secret =
"c8323b026ed8179d35df8471f4550fa6b870e6252e3f014adbc08d2991f99acd";

  const body = { timestamp: timeStamp };
  const payload = Buffer.from(JSON.stringify(body)).toString();
  const signature = crypto
    .createHmac("sha256", secret)
    .update(payload)
    .digest("hex");

  const headers: Record<string, string> = {
    ...rh,
    "X-AUTH-APIKEY": key,
    "X-AUTH-SIGNATURE": signature,
    "X-AUTH-TIMESTAMP": timeStamp.toString(),
    "Content-Type": "application/json",
    "x-host": host || "localhost:3000",
  };

  // console.log("==== DEBUG fetchPageData ====");
 // console.log("UID:", uid);
  // console.log("Base URL:", baseUrl);
  // console.log("Final URL:", `${baseUrl}/page/fetch-single-page/${uid}`);
  // console.log("Timestamp:", timeStamp);
  // console.log("Headers:", headers);
  // console.log("============================");

  try {
    const response = await axios.get(
      `${baseUrl}/page/fetch-single-page/${uid}`,
      { headers },
    );
    // console.log("API Response:", response.data);
    return response.data;
  } catch (error: any) {
    // console.error("Fetch error details:", {
    //   message: error.message,
    //   status: error.response?.status,
    //   statusText: error.response?.statusText,
    //   data: error.response?.data,
    // });
    // console.log("LLLLLLLLLLL", error);
    throw new Error("Failed to fetch Page data.");
  }
}
