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

interface OperatorCardProps {
  company: CompanyWithStats;
}

const OperatorCard = ({ company }: OperatorCardProps) => {
  const navigate = useNavigate();

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

  const getCompanyIcon = () => {
    return <Building className="w-5 h-5" />;
  };

  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 transition-all duration-300 cursor-pointer hover:scale-[1.02]"
      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" />
            
            {/* Park Operator Badge */}
            <div className="absolute top-3 right-3 z-10">
              <Badge variant="outline" className="bg-background/80 backdrop-blur-sm">
                Park Operator
              </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="w-12 h-12 text-muted-foreground/30">
                  {getCompanyIcon()}
                </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-semibold group-hover:text-primary transition-colors line-clamp-2 break-words">
            {company.name}
          </h3>
        </div>

        {/* Company Info */}
        <div className="flex flex-wrap gap-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 flex-shrink-0" />
              <span className="text-muted-foreground truncate">
                {company.headquarters_location}
              </span>
            </div>
          )}
        </div>

        {/* Park Count Stats */}
        <div className="flex items-center justify-between text-sm">
          <div className="flex flex-wrap gap-3 gap-y-1">
          {company.park_count && company.park_count > 0 && (
            <div className="flex items-center gap-1">
              <Building className="w-4 h-4 text-primary/70 flex-shrink-0" />
              <span className="font-medium">{company.park_count}</span>
              <span className="text-muted-foreground">parks</span>
            </div>
          )}
          </div>
          
          {company.average_rating != null && 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 != null && company.review_count > 0 && (
                <span className="text-muted-foreground">({company.review_count})</span>
              )}
            </div>
          )}
        </div>
      </CardContent>
    </Card>
  );
};

export default OperatorCard;
