import { Shop } from "@/types/shop";
import React, { memo } from "react";
import Link from "next/link";
import { ImageWithFallBack } from "@/components/image";
import Image from "next/image";
import MapPinIcon from "@/assets/icons/map-pin";
import { useT } from "@/context/translation-map";
import { createRatingText } from "@/utils/create-rating-text";
import clsx from "clsx";
import VerifiedIcon from "@/assets/icons/verified";

interface ShopCardProps {
  data: Shop;
}

export const ShopCard = memo(
  ({ data }: ShopCardProps) => {
    const t = useT();
    return (
      <div className="relative group border-b border-gray-link pb-4">
        <Link
          href={`/shops/${data.slug}`}
          className="flex gap-3 flex-col sm:flex-row"
          target="_blank"
        >
          <div className="relative sm:h-[140px] aspect-[345/190] sm:aspect-[190/140] rounded-button overflow-hidden">
            <ImageWithFallBack
              src={data.background_img}
              alt={data.translation?.title || ""}
              fill
              className="object-cover transition-all group-hover:scale-105"
            />
            <div className="absolute top-3 right-3 rounded-button border border-white w-7 h-7 flex items-center justify-center z-[1] bg-white bg-opacity-60">
              <span className="text-sm font-semibold" suppressHydrationWarning>
                {data.r_avg || 0}
              </span>
            </div>
          </div>
          <div className="flex flex-col justify-between gap-3 flex-1">
            <div className="flex items-start justify-between">
              <div>
                <div className="flex items-center gap-2">
                  <strong
                    className={clsx(
                      "md:text-lg text-base font-semibold",
                      data?.verify && "line-clamp-1"
                    )}
                    suppressHydrationWarning
                  >
                    {data.translation?.title}
                  </strong>
                  {data?.verify && (
                    <span>
                      <VerifiedIcon />
                    </span>
                  )}
                </div>
                <span className="text-sm text-gray-field line-clamp-2" suppressHydrationWarning>
                  {data.translation?.description}
                </span>
              </div>
              <div className="rounded-full relative w-10 h-10 flex items-center justify-center z-[1] aspect-square flex-shrink-0">
                <Image
                  src={data.logo_img || "/img/image-load-failed.png"}
                  alt={data.translation?.title || "shoplogo"}
                  fill
                  unoptimized
                  className="rounded-full object-cover w-9 h-9"
                />
              </div>
            </div>
            <div>
              <div className="flex items-center gap-1">
                <MapPinIcon />
                <span className="text-xs text-gray-field" suppressHydrationWarning>
                  {data?.distance} {t("km.away.from.you")}
                </span>
              </div>
              <div className="flex items-center gap-2 mt-3">
                <span className="text-sm font-medium" suppressHydrationWarning>
                  {createRatingText(data.r_avg)}
                </span>
                <div className="bg-footerBg rounded-full w-1 h-1" />
                <span className="text-sm font-normal" suppressHydrationWarning>
                  {data.r_count || 0} {t("reviews")}
                </span>
              </div>
            </div>
          </div>
        </Link>
      </div>
    );
  },
  (prev, next) => prev?.data?.id === next?.data?.id
);
