"use client";

import ButtonLink from "@/components/ui/ButtonLink";
import { useTranslation } from "react-i18next";
import Carousel from "react-multi-carousel";
import "react-multi-carousel/lib/styles.css";
import Loader from "@/components/ui/Loader";
import QueryErrorNotice from "@/components/shared/QueryErrorNotice";
import { responsive } from "./partnerCarousel";
import { usePartnerSponsors } from "./usePartnerSponsors";
import SponsorLogo from "./SponsorLogo";

export default function Partner() {
  const { t } = useTranslation();
  const {
    isLoading,
    loadError,
    retry,
    sponsorTypeLoading,
    itemsPerBreakpoint,
    getColorForSponsorType,
    filteredSponsorCategories,
  } = usePartnerSponsors();

  return (
    <>
      {!isLoading && !sponsorTypeLoading ? (
        <section
          className="bg-[#F7F7FB] py-8 md:py-10 sponsersitm dotlist-white"
        >
          <div className="mx-auto w-[92%] max-w-[1080px]">
            <h1 className="mb-7 text-center text-2xl md:text-3xl font-bold text-primary-5">
              {t("COMMON.PARTNERS.IN.SUCCESS")}
            </h1>
            {loadError ? (
              <QueryErrorNotice error={loadError} onRetry={retry} />
            ) : filteredSponsorCategories?.length > 0 ? (
              filteredSponsorCategories?.map((category, categoryIndex) => (
                <div key={categoryIndex} className="mb-4 rounded-2xl border border-primary-5/10 bg-white px-4 py-5 sm:px-6 sm:py-6">
                  <h3
                    className="mb-4 text-center text-lg sm:text-xl font-bold"
                    style={{
                      color: getColorForSponsorType(category.type),
                    }}
                  >
                    {category.subtitle}
                  </h3>

                  <div className="flex justify-center relative">
                    <div className="w-full pb-3">
                      {category.sponsors.length > (itemsPerBreakpoint || 3) ? (
                        <div className="flex flex-wrap items-center justify-center gap-4 sm:gap-8">
                          <Carousel
                            responsive={responsive}
                            infinite={true}
                            autoPlay={true}
                            showDots={true}
                            autoPlaySpeed={2000}
                            keyBoardControl={true}
                            customTransition="all .5s"
                            transitionDuration={500}
                            containerClass="carousel-container w-full"
                            arrows={false}
                            dotListClass="custom-dot-list-style"
                            itemClass="flex justify-center items-center px-2 sm:px-4"
                            centerMode={false}
                            partialVisible={false}
                          >
                            {category.sponsors.map((sponsor, sponsorIndex) => (
                              <SponsorLogo
                                key={sponsorIndex}
                                sponsor={sponsor}
                                wrapperClassName="flex justify-center items-center w-full"
                              />
                            ))}
                          </Carousel>
                        </div>
                      ) : (
                        <div className="flex flex-wrap items-center justify-center gap-4 sm:gap-8">
                          {category.sponsors.map((sponsor, sponsorIndex) => (
                            <SponsorLogo
                              key={sponsorIndex}
                              sponsor={sponsor}
                              wrapperClassName="flex items-center justify-center px-2 sm:px-4"
                            />
                          ))}
                        </div>
                      )}
                    </div>
                  </div>

                </div>
              ))
            ) : (
              <p className="text-center mb-5">{t("COMMON.NO_SPONSORS")}</p>
            )}

            <div className="mt-6 flex justify-center">
              {/* One element: a link wearing the button's styling. */}
              <ButtonLink
                href="/sponsorship-form"
                variant="primary"
                className="!w-[220px]"
                size="medium"
              >
                {t("COMMON.JOIN.OUR.PARTNERS")}
              </ButtonLink>
            </div>
          </div>
        </section>
      ) : (
        <Loader />
      )}
    </>
  );
}
