'use client';

import Link from 'next/link';
import AppImage from '@/components/ui/AppImage';
import { MessageCircle, ArrowRight } from 'lucide-react';
import { weeklyOffers } from '@/lib/offers';
import { PAGE_IMAGES } from '@/infrastructure/images';
import { whatsappOrderUrl } from '@/lib/constants';
import { formatPrice, getFormatLocale } from '@/lib/utils';
import { useTranslation } from '@/hooks/useTranslation';

export default function WeeklyOffers() {
  const { t, locale } = useTranslation();
  const fmt = (n: number) => formatPrice(n, getFormatLocale(locale));

  return (
    <section className="bg-cream-100 py-20">
      <div className="mx-auto max-w-7xl px-4 sm:px-6 lg:px-8">
        <div className="grid items-center gap-12 lg:grid-cols-2 lg:gap-16">
          <div>
            <span className="mb-2 inline-block text-sm font-semibold uppercase tracking-wider text-gold-600">
              {t('offers.label')}
            </span>
            <h2 className="section-heading mb-3">{t('offers.title')}</h2>
            <p className="mb-8 text-gray-500">{t('offers.subtitle')}</p>

            <div className="space-y-4">
              {weeklyOffers.map((offer) => (
                <div
                  key={offer.id}
                  className="group flex gap-4 rounded-2xl border border-cream-300/80 bg-white p-4 shadow-premium transition-all duration-300 hover:border-brand-200 hover:shadow-premium-lg sm:p-5"
                >
                  <Link
                    href={`/product/${offer.productSlug}`}
                    className="relative h-20 w-20 shrink-0 overflow-hidden rounded-xl sm:h-24 sm:w-24"
                  >
                    <AppImage
                      src={offer.image}
                      alt={t(`${offer.nameKey}.name`)}
                      fill
                      className="object-cover transition-transform duration-500 group-hover:scale-105"
                      sizes="(max-width: 640px) 96px, 128px"
                    />
                  </Link>

                  <div className="min-w-0 flex-1">
                    <div className="mb-1 flex flex-wrap items-center gap-2">
                      <span
                        className={
                          offer.badgeKey === 'halal'
                            ? 'rounded-full bg-brand-700 px-2.5 py-0.5 text-[10px] font-bold uppercase tracking-wider text-white'
                            : 'rounded-full bg-gold-500 px-2.5 py-0.5 text-[10px] font-bold uppercase tracking-wider text-white'
                        }
                      >
                        {t(`offers.badge.${offer.badgeKey}`)}
                      </span>
                    </div>

                    <Link href={`/product/${offer.productSlug}`}>
                      <h3 className="mb-2 font-display text-lg font-bold text-brand-900 transition-colors hover:text-brand-700">
                        {t(`${offer.nameKey}.name`)}
                      </h3>
                    </Link>

                    <div className="flex flex-wrap items-end justify-between gap-3">
                      <div>
                        {offer.originalPrice != null && (
                          <p className="text-sm text-gray-400 line-through">
                            {t('offers.was')}: {fmt(offer.originalPrice)}/
                            {t(`priceUnit.${offer.priceUnitKey}`)}
                          </p>
                        )}
                        <p className="font-display text-xl font-bold text-brand-800">
                          {offer.originalPrice != null && (
                            <span className="me-2 text-sm font-semibold uppercase text-gold-600">
                              {t('offers.now')}
                            </span>
                          )}
                          {fmt(offer.price)}
                          <span className="text-sm font-normal text-gray-500">
                            /{t(`priceUnit.${offer.priceUnitKey}`)}
                          </span>
                        </p>
                      </div>

                      <div className="flex flex-wrap gap-2">
                        <Link
                          href={`/product/${offer.productSlug}`}
                          className="inline-flex items-center gap-1 rounded-lg border border-brand-200 px-3 py-2 text-xs font-semibold text-brand-700 transition-colors hover:bg-brand-50"
                        >
                          {t('offers.viewProduct')}
                          <ArrowRight className="h-3 w-3 rtl:rotate-180" />
                        </Link>
                        <a
                          href={whatsappOrderUrl(offer.whatsappProduct)}
                          target="_blank"
                          rel="noopener noreferrer"
                          className="inline-flex items-center gap-1.5 rounded-lg bg-brand-700 px-4 py-2 text-xs font-semibold text-white transition-colors hover:bg-brand-800"
                        >
                          <MessageCircle className="h-3.5 w-3.5" />
                          {t('offers.order')}
                        </a>
                      </div>
                    </div>
                  </div>
                </div>
              ))}
            </div>

            <p className="mt-6 text-xs text-gray-400">{t('offers.disclaimer')}</p>
          </div>

          <Link
            href="/shop"
            className="relative hidden aspect-[4/5] overflow-hidden rounded-2xl shadow-premium-lg lg:block"
          >
            <AppImage
              src={PAGE_IMAGES.weeklyOffersBanner}
              alt=""
              fill
              className="object-cover transition-transform duration-500 hover:scale-105"
              sizes="(max-width: 1024px) 0px, 640px"
            />
            <div className="absolute inset-0 bg-gradient-to-t from-brand-950/50 via-transparent to-transparent" />
          </Link>
        </div>
      </div>
    </section>
  );
}