'use client';

import React, { useEffect, useState } from 'react';
import Link from 'next/link';
import Image from 'next/image';
import { ArrowRight, Sparkles } from 'lucide-react';

interface RelatedArticle {
  id: number;
  slug: string;
  title: string;
  imageUrl: string | null;
  category: string;
}

interface RelatedInContentProps {
  currentCategory: string;
  currentId: number;
}

export default function RelatedInContent({ currentCategory, currentId }: RelatedInContentProps) {
  const [article, setArticle] = useState<RelatedArticle | null>(null);

  useEffect(() => {
    // İlgili haberi çek
    const fetchRelated = async () => {
      try {
        const res = await fetch(`/api/articles?category=${currentCategory}&exclude=${currentId}&limit=1`);
        const data = await res.json();
        if (data.articles && data.articles.length > 0) {
          setArticle(data.articles[0]);
        }
      } catch {
        // Hata durumunda boş bırak
      }
    };

    fetchRelated();
  }, [currentCategory, currentId]);

  if (!article) return null;

  return (
    <div className="my-10 p-5 bg-gradient-to-r from-red-50 via-orange-50 to-amber-50 rounded-2xl border-2 border-red-100 relative overflow-hidden">
      {/* Decorative */}
      <div className="absolute top-0 right-0 w-24 h-24 bg-gradient-to-br from-red-200/40 to-orange-200/40 rounded-full blur-2xl -translate-y-1/2 translate-x-1/2" />

      <div className="relative flex gap-4 items-start">
        {/* Image */}
        {article.imageUrl && (
          <Link href={`/haber/${article.slug}`} className="flex-shrink-0">
            <div className="relative w-24 h-24 md:w-32 md:h-32 rounded-xl overflow-hidden bg-gray-100">
              <Image
                src={article.imageUrl}
                alt={article.title}
                fill
                className="object-cover hover:scale-105 transition-transform duration-300"
                sizes="128px"
              />
            </div>
          </Link>
        )}

        {/* Content */}
        <div className="flex-1 min-w-0">
          <div className="flex items-center gap-2 mb-2">
            <Sparkles size={14} className="text-red-500" />
            <span className="text-[10px] font-bold text-red-600 uppercase tracking-wider">
              İlginizi Çekebilir
            </span>
          </div>

          <Link href={`/haber/${article.slug}`}>
            <h4 className="font-bold text-gray-900 text-sm md:text-base leading-snug hover:text-red-600 transition-colors line-clamp-3">
              {article.title}
            </h4>
          </Link>

          <Link
            href={`/haber/${article.slug}`}
            className="inline-flex items-center gap-1 mt-3 text-xs font-semibold text-red-600 hover:text-red-700 transition-colors"
          >
            İçeriği Görüntüle
            <ArrowRight size={14} />
          </Link>
        </div>
      </div>
    </div>
  );
}
