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

interface ManufacturerCardProps {
  company: CompanyWithStats;
}

export function ManufacturerCard({ company }: ManufacturerCardProps) {
  const navigate = useNavigate();
  
  const handleClick = () => {
    const basePath = company.company_type === 'designer' ? '/designers' : '/manufacturers';
    navigate(`${basePath}/${company.slug}/`);
  };

  const getCompanyIcon = (type: string) => {
    switch (type) {
      case 'manufacturer': return <Factory className="w-5 h-5" />;
      case 'operator': return <FerrisWheel className="w-5 h-5" />;
      case 'designer': return <Ruler className="w-5 h-5" />;
      case 'contractor': return <Hammer className="w-5 h-5" />;
      default: return <Building2 className="w-5 h-5" />;
    }
  };

  const formatCompanyType = (type: string) => {
    return type.split('_').map(word => 
      word.charAt(0).toUpperCase() + word.slice(1)
    ).join(' ');
  };

  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}
    >
      {/* Logo/Image Section */}
      <div className="aspect-[3/2] relative bg-gradient-to-br from-primary/20 via-primary/10 to-transparent overflow-hidden">
        {(company.card_image_url || company.card_image_id) ? (
          <img 
            src={company.card_image_url || getCloudflareImageUrl(company.card_image_id, 'card')} 
            srcSet={company.card_image_id ? `
              ${getCloudflareImageUrl(company.card_image_id, 'cardthumb')} 600w,
              ${getCloudflareImageUrl(company.card_image_id, 'card')} 1200w
            ` : undefined}
            sizes="(max-width: 640px) 600px, 1200px"
            alt={company.name}
            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-background/80 via-transparent to-transparent" />
            
            {/* Company Type Badge */}
            <div className="absolute top-3 right-3 z-10">
              <Badge variant="outline" className="bg-background/80 backdrop-blur-sm text-xs">
                {formatCompanyType(company.company_type)}
              </Badge>
            </div>

            {/* Logo Display */}
            <div className="absolute inset-0 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">
                    <div className="w-8 h-8 text-primary/70">
                      {getCompanyIcon(company.company_type)}
                    </div>
                  </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="text-base font-bold 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 Info */}
        <div className="flex flex-wrap gap-x-3 gap-y-1 text-sm">
          {company.founded_year && (
            <div className="flex items-center gap-1">
              <span className="text-muted-foreground">Founded:</span>
              <span className="font-medium">{company.founded_year}</span>
            </div>
          )}
          
          {company.headquarters_location && (
            <div className="flex items-center gap-1 min-w-0">
              <MapPin className="w-3 h-3 shrink-0" />
              <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">rides</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">models</span>
            </div>
          )}
          </div>
          
          {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 > 0 && (
                <span className="text-muted-foreground">({company.review_count})</span>
              )}
            </div>
          )}
        </div>
      </CardContent>
    </Card>
  );
}