"use client";

import { useQuery } from "@tanstack/react-query";
import { useTranslation } from "react-i18next";
import Loader from "@/components/ui/Loader";
import { getQRCode } from "@/features/profile/services/profileApi";
import { useLanguageStore } from "@/store/languageStore";
import RemoteImage from "@/components/ui/RemoteImage";

interface QRCodeData {
  qr_code_url?: string;
  name?: string;
  manual_id?: string;
  volunteer_id?: number;
}

export default function QRCode() {
  const { t } = useTranslation();
  const language = useLanguageStore((s) => s.language);

  const { data, isLoading, error } = useQuery<QRCodeData>({
    queryKey: ["volunteer-qr-code"],
    queryFn: async () => {
      const response = await getQRCode();
      // The endpoint returns the payload either at the root or under `data`.
      return response?.data ?? response ?? {};
    },
  });

  if (isLoading) {
    return <Loader />;
  }

  if (error) {
    return <div>{t("COMMON.ERROR_LOADING_QR_CODE")}</div>;
  }

  const qrCodeUrl = data?.qr_code_url || "";
  const name = data?.name || "";
  const manualId = data?.manual_id || "";

  return (
    <div className="">
      <div className="2xl:w-[75%] laptopmain:w-[83%] laptop:w-[78%] laptopitm:w-[85%] lg:w-[90%] md:w-[85%] w-[90%] mx-auto relative 2xl:py-[40px] laptopmain:py-[50px] laptop:py-[40px] lg:py-[40px] py-[40px]">
        <div className="">
          <div className="flex justify-center mb-8">
            {/* A QR code is square by construction, so 300 — the base rendered
                height in the classes — states the aspect box honestly while
                the classes keep deciding the painted size. It is also the
                only thing on this screen: unsized, the whole page jumped when
                it arrived, which is what the previous "not layout-critical"
                note got wrong. `images.unoptimized: true` means the request
                is the same single fetch either way. */}
            <RemoteImage
              src={qrCodeUrl}
              alt={t("COMMON.QR_CODE")}
              width={300}
              height={300}
              className="2x:h-[433px] lg:h-[300px] md:h-[300px] h-auto w-auto border-[6px] border-[#29246D] rounded-[16px] qrscreenswidth"
            />
          </div>
          <div className="text-lg text-primary-5 flex justify-center">
            <div className={language === "ar" ? "text-right" : "text-left"}>
              <p className="font-semibold">
                {t("COMMON.NAME")}
                <span className="pl-2">:</span>
                <span className="pl-2 font-normal">{name}</span>
              </p>
              <p className="font-semibold mt-2">
                {t("COMMON.ID")}
                <span className="pl-2">:</span>
                <span className="pl-2 font-normal">{manualId}</span>
              </p>
            </div>
          </div>
        </div>
      </div>
    </div>
  );
}
