import { Card, CardContent } from '@/components/ui/card';
import { Badge } from '@/components/ui/badge';
import { Star, MapPin, Ruler, FerrisWheel } from 'lucide-react';
import { CompanyWithStats } from '@/types/database';
import { useNavigate } from 'react-router-dom';
import { getCloudflareImageUrl } from '@/lib/cloudflareImageUtils';

interface DesignerCardProps {
  company: CompanyWithStats;
}

export function DesignerCard({ company }: DesignerCardProps) {
  const navigate = useNavigate();

  const handleClick = () => {
    navigate(`/designers/${company.slug}/`);
  };

  const getCompanyIcon = () => {
    return <Ruler className="w-8 h-8 text-muted-foreground" />;
  };

  return (
    <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 cursor-pointer 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"
      onClick={handleClick}
    >
      {/* Header Image/Logo Section */}
      <div className="relative aspect-[3/2] bg-gradient-to-br from-primary/20 via-secondary/20 to-accent/20 flex items-center justify-center overflow-hidden">
        {/* Background Gradient Overlay */}
        <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" />
        
        {/* Company Type Badge */}
        <div className="absolute top-3 right-3">
          <Badge variant="secondary" className="text-xs bg-background/80 backdrop-blur-sm border border-border/50">
            Designer
          </Badge>
        </div>

        {/* Logo or Icon */}
        <div className="relative z-10 flex items-center justify-center">
          {company.logo_url ? (
            <div className="w-12 h-12 md:w-16 md:h-16 bg-background/90 rounded-xl overflow-hidden shadow-lg backdrop-blur-sm border border-border/50">
              <img 
                src={company.logo_url} 
                alt={`${company.name} logo`}
                className="w-full h-full object-contain p-2"
                loading="lazy"
              />
            </div>
          ) : (
            <div className="relative">
              <div className="absolute inset-0 rounded-full bg-primary/20 blur-xl animate-pulse" />
              <div className="relative w-16 h-16 rounded-full bg-gradient-to-br from-primary/30 to-secondary/30 flex items-center justify-center border border-primary/20">
                <Ruler className="w-8 h-8 text-primary/70" />
              </div>
            </div>
          )}
        </div>
      </div>

      <CardContent className="p-2.5 space-y-1.5 border-t border-border/30">
        {/* Company Name */}
        <div className="space-y-0.5 min-w-0">
          <h3 className="font-bold text-base group-hover:text-primary transition-all duration-300 line-clamp-2 break-words group-hover:drop-shadow-[0_0_8px_rgba(139,92,246,0.5)]">
            {company.name}
          </h3>
        </div>

        {/* Company Details */}
        <div className="space-y-1 text-sm">
          {/* Founded Year */}
          {company.founded_year && (
            <div className="flex items-center gap-2">
              <span className="text-muted-foreground">Founded:</span>
              <span className="font-medium">{company.founded_year}</span>
            </div>
          )}
          
          {/* Location */}
          {company.headquarters_location && (
            <div className="flex items-center gap-1 min-w-0">
              <MapPin className="w-3 h-3 flex-shrink-0 text-muted-foreground" />
              <span className="text-muted-foreground truncate">{company.headquarters_location}</span>
            </div>
          )}
        </div>

        {/* Stats Display */}
        <div className="flex items-center justify-between text-sm">
          <div className="flex flex-wrap gap-3 gap-y-1">
          {company.ride_count && company.ride_count > 0 && (
            <div className="flex items-center gap-1">
              <FerrisWheel className="w-4 h-4 text-primary/70 flex-shrink-0" />
              <span className="font-medium">{company.ride_count}</span>
              <span className="text-muted-foreground">designs</span>
            </div>
          )}
          
          {company.coaster_count && company.coaster_count > 0 && (
            <div className="flex items-center gap-1">
              <span className="font-medium">{company.coaster_count}</span>
              <span className="text-muted-foreground">coasters</span>
            </div>
          )}
          
          {company.model_count && company.model_count > 0 && (
            <div className="flex items-center gap-1">
              <span className="font-medium">{company.model_count}</span>
              <span className="text-muted-foreground">concepts</span>
            </div>
          )}
          </div>
          
          {company.average_rating && company.average_rating > 0 && (
            <div className="inline-flex items-center gap-1">
              <Star className="w-4 h-4 fill-yellow-500 text-yellow-500" />
              <span className="font-semibold">{company.average_rating.toFixed(1)}</span>
              {company.review_count && company.review_count > 0 && (
                <span className="text-muted-foreground">({company.review_count})</span>
              )}
            </div>
          )}
        </div>
      </CardContent>
    </Card>
  );
}