import { Link } from "wouter";
import { WorkWithAuthor } from "@/lib/types";
import { Heart } from "lucide-react";
import { Button } from "@/components/ui/button";
import { Badge } from "@/components/ui/badge";
import { LanguageTag } from "@/components/common/LanguageTag";
import { useState } from "react";
import { apiRequest } from "@/lib/queryClient";
import { useToast } from "@/hooks/use-toast";

interface WorkCardProps {
  work: WorkWithAuthor;
  compact?: boolean;
  grid?: boolean;
}

export function WorkCard({ work, compact = false, grid = false }: WorkCardProps) {
  const [likeCount, setLikeCount] = useState<number>(work.likes || 0);
  const [isLiked, setIsLiked] = useState<boolean>(false);
  const { toast } = useToast();

  const handleLike = async (e: React.MouseEvent) => {
    e.preventDefault();
    e.stopPropagation();
    
    try {
      if (!isLiked) {
        await apiRequest('POST', '/api/likes', {
          userId: 1, // For demo, we'll use hardcoded user ID
          entityType: 'work',
          entityId: work.id
        });
        setLikeCount(likeCount + 1);
        setIsLiked(true);
      } else {
        // In a real app, we would delete the specific like
        setLikeCount(likeCount - 1);
        setIsLiked(false);
      }
    } catch (error) {
      toast({
        title: "Error",
        description: "Could not like this work. Please try again.",
        variant: "destructive"
      });
    }
  };

  if (grid) {
    return (
      <Link href={`/works/${work.slug}`}>
        <div className="card group bg-cream dark:bg-dark-surface p-4 rounded-lg shadow-sm border border-sage/10 dark:border-sage/5 flex flex-col h-full transition-shadow hover:shadow-md">
          <div className="mb-3">
            <h3 className="font-serif text-lg font-semibold mb-1 group-hover:text-russet dark:group-hover:text-russet/90 transition-colors">
              {work.title}
            </h3>
            <p className="text-sm text-navy/70 dark:text-cream/70">{work.author?.name || 'Unknown Author'}</p>
          </div>
          <div className="flex flex-wrap gap-2 mb-3">
            {work.tags && work.tags.length > 0 ? (
              work.tags.slice(0, 3).map((tag) => (
                <Badge key={tag.id} variant="outline" className="bg-navy/10 dark:bg-navy/20 text-navy/70 dark:text-cream/70 text-xs border-none">
                  {tag.name}
                </Badge>
              ))
            ) : (
              <Badge variant="outline" className="bg-navy/10 dark:bg-navy/20 text-navy/70 dark:text-cream/70 text-xs border-none">
                General
              </Badge>
            )}
          </div>
          <p className="text-sm text-navy/80 dark:text-cream/80 mb-3 line-clamp-3 flex-1">
            {work.description}
          </p>
          <div className="flex items-center justify-between mt-auto pt-3 border-t border-sage/10 dark:border-sage/5">
            <LanguageTag language={`${work.language}, ${work.year || 'Unknown'}`} />
            <span className="text-xs text-navy/60 dark:text-cream/60">
              {/* Assuming translations count would be available */}
              {Math.floor(Math.random() * 10) + 1} translations
            </span>
          </div>
        </div>
      </Link>
    );
  }

  return (
    <div className="card bg-cream dark:bg-dark-surface p-4 rounded-lg shadow-sm border border-sage/10 dark:border-sage/5 flex flex-col sm:flex-row sm:items-center gap-4">
      <div className="flex-1">
        <h3 className="font-serif text-lg font-semibold mb-1">
          <Link href={`/works/${work.slug}`} className="text-navy dark:text-cream hover:text-russet dark:hover:text-russet/90 transition-colors">
            {work.title}
          </Link>
        </h3>
        <div className="flex flex-wrap items-center gap-x-3 gap-y-1 mb-2">
          <span className="text-xs text-navy/70 dark:text-cream/70 font-sans">
            {work.type === 'poem' ? 'Poem' : 
             work.type === 'story' ? 'Short story' : 
             work.type === 'novel' ? 'Novel' : 
             work.type === 'play' ? 'Play' : 
             work.type === 'essay' ? 'Essay' : 'Work'}, {work.year}
          </span>
          <LanguageTag language={work.language} />
        </div>
        
        {!compact && (
          <p className="text-navy/80 dark:text-cream/80 text-sm mb-2 line-clamp-2">
            {work.description}
          </p>
        )}
        
        <div className="flex flex-wrap gap-2">
          {work.tags && work.tags.length > 0 ? (
            work.tags.map((tag) => (
              <Badge key={tag.id} variant="outline" className="inline-block px-2 py-0.5 bg-navy/10 dark:bg-navy/20 rounded text-navy/70 dark:text-cream/70 text-xs font-sans border-none">
                {tag.name}
              </Badge>
            ))
          ) : (
            <Badge variant="outline" className="inline-block px-2 py-0.5 bg-navy/10 dark:bg-navy/20 rounded text-navy/70 dark:text-cream/70 text-xs font-sans border-none">
              General
            </Badge>
          )}
        </div>
      </div>
      <div className="flex items-center sm:flex-col gap-3 sm:gap-2 self-end sm:self-center">
        <div className="flex items-center gap-1">
          <span className="text-xs text-navy/70 dark:text-cream/70 font-sans">
            {/* Assuming translations count would be available */}
            {Math.floor(Math.random() * 10) + 1} translations
          </span>
        </div>
        <Button
          size="sm"
          variant="outline"
          className={`btn-feedback flex items-center gap-1 py-1 px-2.5 rounded-full ${
            isLiked 
              ? 'bg-russet/20 hover:bg-russet/30 dark:bg-russet/30 dark:hover:bg-russet/40 text-russet dark:text-russet/90' 
              : 'bg-russet/10 hover:bg-russet/20 dark:bg-russet/20 dark:hover:bg-russet/30 text-russet dark:text-russet/90'
          } font-sans text-xs transition-colors`}
          onClick={handleLike}
        >
          <Heart className={`h-3.5 w-3.5 ${isLiked ? 'fill-russet' : ''}`} />
          <span>{likeCount}</span>
        </Button>
      </div>
    </div>
  );
}
