import React from 'react';
import Link from 'next/link';
import Image from 'next/image';
import { Clock, Eye, Share2 } from 'lucide-react';
import { Article } from '@/types';

interface NewsCardProps {
  article: Article;
  featured?: boolean;
}

function formatDate(date: Date | string): string {
  const d = new Date(date);
  const now = new Date();
  const diff = now.getTime() - d.getTime();
  const minutes = Math.floor(diff / 60000);
  const hours = Math.floor(diff / 3600000);
  const days = Math.floor(diff / 86400000);

  if (minutes < 60) return `${minutes} dakika önce`;
  if (hours < 24) return `${hours} saat önce`;
  if (days < 7) return `${days} gün önce`;
  return d.toLocaleDateString('tr-TR');
}

function estimateReadTime(content: string): string {
  const words = content.split(/\s+/).length;
  const minutes = Math.ceil(words / 200);
  return `${minutes} dk`;
}

export default function NewsCard({ article, featured }: NewsCardProps) {
  const imageUrl = article.imageUrl || '/images/placeholder.jpg';

  if (featured) {
    return (
      <Link
        href={`/haber/${article.slug}`}
        className="relative group cursor-pointer overflow-hidden rounded-2xl h-[400px] md:h-[500px] block bg-gray-100"
      >
        <Image
          src={imageUrl}
          alt={article.title}
          fill
          className="object-cover transition-transform duration-700 group-hover:scale-105"
          sizes="(max-width: 768px) 100vw, 50vw"
          priority
          placeholder="blur"
          blurDataURL="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAARCAAIAAoDASIAAhEBAxEB/8QAFgABAQEAAAAAAAAAAAAAAAAAAAUH/8QAIhAAAgEDAwUBAAAAAAAAAAAAAQIDAAQRBRIhBhMxQVFh/8QAFQEBAQAAAAAAAAAAAAAAAAAAAAX/xAAZEQACAwEAAAAAAAAAAAAAAAABAgADESH/2gAMAwEAAhEDEQA/AKuo9V6jqNy1zdXssk0m0O7EZbaMDOPQwB+UpSq1szDSCxm5Z//Z"
        />
        <div className="absolute inset-0 bg-gradient-to-t from-black/90 via-black/40 to-transparent" />
        <div className="absolute bottom-0 left-0 p-6 md:p-8 w-full">
          <div className="flex gap-2 mb-3">
            <span className="px-3 py-1 bg-red-600 text-white text-[10px] font-bold uppercase rounded-full">
              {article.category}
            </span>
          </div>
          <h2 className="text-2xl md:text-4xl font-extrabold text-white leading-tight mb-4 group-hover:text-red-100 transition-colors">
            {article.title}
          </h2>
          <div className="flex items-center gap-4 text-gray-300 text-xs font-medium">
            <span className="flex items-center gap-1">
              <Clock size={14} /> {formatDate(article.publishedAt)}
            </span>
            <span className="flex items-center gap-1">
              <Eye size={14} /> {article.viewCount.toLocaleString('tr-TR')} görüntülenme
            </span>
          </div>
        </div>
      </Link>
    );
  }

  return (
    <article className="news-card group flex flex-col h-full">
      <Link href={`/haber/${article.slug}`} className="block">
        <div className="relative h-48 overflow-hidden bg-gray-100">
          <Image
            src={imageUrl}
            alt={article.title}
            fill
            className="object-cover transition-transform duration-500 group-hover:scale-105"
            sizes="(max-width: 768px) 100vw, (max-width: 1200px) 50vw, 33vw"
            placeholder="blur"
            blurDataURL="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAARCAAIAAoDASIAAhEBAxEB/8QAFgABAQEAAAAAAAAAAAAAAAAAAAUH/8QAIhAAAgEDAwUBAAAAAAAAAAAAAQIDAAQRBRIhBhMxQVFh/8QAFQEBAQAAAAAAAAAAAAAAAAAAAAX/xAAZEQACAwEAAAAAAAAAAAAAAAABAgADESH/2gAMAwEAAhEDEQA/AKuo9V6jqNy1zdXssk0m0O7EZbaMDOPQwB+UpSq1szDSCxm5Z//Z"
          />
          <span className="absolute top-2 left-2 px-2 py-1 bg-red-600 text-white text-[10px] font-bold uppercase rounded">
            {article.category}
          </span>
        </div>
      </Link>
      <div className="p-4 flex flex-col flex-1">
        <div className="flex items-center justify-between mb-2 text-[10px] font-bold text-gray-400 uppercase tracking-widest">
          <span>{formatDate(article.publishedAt)}</span>
          <span>{estimateReadTime(article.content)} OKUMA</span>
        </div>
        <Link href={`/haber/${article.slug}`}>
          <h3 className="text-lg font-bold text-gray-900 mb-2 group-hover:text-red-600 transition-colors leading-snug line-clamp-2">
            {article.title}
          </h3>
        </Link>
        <p className="text-sm text-gray-600 line-clamp-2 mb-4">{article.summary}</p>
        <div className="mt-auto pt-4 border-t border-gray-50 flex items-center justify-between">
          <div className="flex items-center gap-2">
            <span className="text-xs font-semibold text-gray-500">{article.source}</span>
          </div>
          <button
            className="p-1.5 text-gray-400 hover:text-red-600 transition-colors"
            aria-label="Paylaş"
          >
            <Share2 size={16} />
          </button>
        </div>
      </div>
    </article>
  );
}
