import { Link } from 'react-router-dom';
import { Card } from '@/components/ui/card';
import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar';
import { Eye, Calendar } from 'lucide-react';
import { getCloudflareImageUrl } from '@/lib/cloudflareImageUtils';
import { formatDistanceToNow } from 'date-fns';

interface BlogPostCardProps {
  slug: string;
  title: string;
  content: string;
  featuredImageId?: string;
  author: {
    username: string;
    displayName?: string;
    avatarUrl?: string;
  };
  publishedAt: string;
  viewCount: number;
}

export function BlogPostCard({
  slug,
  title,
  content,
  featuredImageId,
  author,
  publishedAt,
  viewCount,
}: BlogPostCardProps) {
  const excerpt = content.substring(0, 150) + (content.length > 150 ? '...' : '');
  
  return (
    <Link to={`/blog/${slug}`}>
      <Card className="group overflow-hidden border-border/50 bg-gradient-to-br from-card via-card to-card/80 hover:shadow-2xl hover:shadow-primary/20 hover:border-primary/30 transition-all duration-300 hover:scale-[1.02] relative before:absolute before:inset-0 before:rounded-lg before:p-[1px] before:bg-gradient-to-br before:from-primary/20 before:via-transparent before:to-accent/20 before:-z-10 before:opacity-0 hover:before:opacity-100 before:transition-opacity before:duration-300">
        <div className="aspect-[3/2] overflow-hidden bg-gradient-to-br from-primary/20 via-secondary/20 to-accent/20 relative">
          {featuredImageId ? (
            <>
              <img
                src={getCloudflareImageUrl(featuredImageId, 'public')}
                alt={title}
                className="w-full h-full object-cover group-hover:scale-110 transition-transform duration-500"
                loading="lazy"
              />
              <div className="absolute inset-0 bg-gradient-to-t from-black/70 via-black/20 to-transparent opacity-80 group-hover:opacity-60 transition-opacity duration-300" />
            </>
          ) : (
            <div className="w-full h-full flex items-center justify-center">
              <div className="relative">
                <div className="absolute inset-0 rounded-full bg-primary/20 blur-xl animate-pulse" />
                <div className="relative w-20 h-20 rounded-full bg-gradient-to-br from-primary/30 to-secondary/30 flex items-center justify-center border border-primary/20">
                  <span className="text-4xl opacity-80">📝</span>
                </div>
              </div>
            </div>
          )}
        </div>

        <div className="p-3 space-y-2 border-t border-border/30">
          <h3 className="text-base font-bold line-clamp-2 group-hover:text-primary transition-all duration-300 group-hover:drop-shadow-[0_0_8px_rgba(139,92,246,0.5)]">
            {title}
          </h3>

          <p className="text-sm text-muted-foreground line-clamp-3">
            {excerpt}
          </p>

          <div className="flex items-center justify-between pt-3 border-t">
            <div className="flex items-center gap-2">
              <Avatar className="w-6 h-6">
                <AvatarImage src={author.avatarUrl} />
                <AvatarFallback>
                  {author.displayName?.[0] || author.username[0]}
                </AvatarFallback>
              </Avatar>
              <span className="text-xs text-muted-foreground">
                {author.displayName || author.username}
              </span>
            </div>

            <div className="flex items-center gap-3 text-xs text-muted-foreground">
              <div className="flex items-center gap-1">
                <Calendar className="w-3 h-3" />
                {formatDistanceToNow(new Date(publishedAt), { addSuffix: true })}
              </div>
              <div className="flex items-center gap-1">
                <Eye className="w-3 h-3" />
                {viewCount}
              </div>
            </div>
          </div>
        </div>
      </Card>
    </Link>
  );
}
