import { useNavigate } from 'react-router-dom';
import { Card, CardContent } from '@/components/ui/card';
import { Badge } from '@/components/ui/badge';
import { Star, MapPin, Clock, Zap, FerrisWheel, Waves, Theater, Train } from 'lucide-react';
import { MeasurementDisplay } from '@/components/ui/measurement-display';
import { Ride } from '@/types/database';

interface RideCardProps {
  ride: Ride;
  showParkName?: boolean;
  className?: string;
}

export function RideCard({ ride, showParkName = true, className }: RideCardProps) {
  const navigate = useNavigate();

  const handleRideClick = () => {
    navigate(`/parks/${ride.park?.slug}/rides/${ride.slug}`);
  };

  const getRideIcon = (category: string) => {
    switch (category) {
      case 'roller_coaster': return <FerrisWheel className="w-6 h-6" />;
      case 'water_ride': return <Waves className="w-6 h-6" />;
      case 'dark_ride': return <Theater className="w-6 h-6" />;
      case 'flat_ride': return <FerrisWheel className="w-6 h-6" />;
      case 'kiddie_ride': return <FerrisWheel className="w-6 h-6" />;
      case 'transportation': return <Train className="w-6 h-6" />;
      default: return <FerrisWheel className="w-6 h-6" />;
    }
  };

  const getStatusColor = (status: string) => {
    switch (status) {
      case 'operating': return 'bg-green-500/20 text-green-400 border-green-500/30';
      case 'seasonal': return 'bg-yellow-500/20 text-yellow-400 border-yellow-500/30';
      case 'under_construction': return 'bg-blue-500/20 text-blue-400 border-blue-500/30';
      default: return 'bg-red-500/20 text-red-400 border-red-500/30';
    }
  };

  const formatCategory = (category: string) => {
    return category.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 transition-all duration-300 cursor-pointer hover:scale-[1.02] ${className}`}
      onClick={handleRideClick}
    >
      <div className="relative overflow-hidden">
        {/* Image/Icon Section */}
        <div className="aspect-video bg-gradient-to-br from-primary/20 via-secondary/20 to-accent/20 flex items-center justify-center relative">
          {ride.image_url ? (
            <img 
              src={ride.image_url} 
              alt={ride.name}
              className="w-full h-full object-cover group-hover:scale-110 transition-transform duration-500"
            />
          ) : (
            <div className="opacity-50 flex items-center justify-center">
              {getRideIcon(ride.category)}
            </div>
          )}
          
          {/* Gradient Overlay */}
          <div className="absolute inset-0 bg-gradient-to-t from-black/60 via-transparent to-transparent" />
          
          {/* Status Badge */}
          <Badge 
            className={`absolute top-3 right-3 ${getStatusColor(ride.status)} border`}
          >
            {ride.status.replace('_', ' ').toUpperCase()}
          </Badge>
        </div>

        <CardContent className="p-4 space-y-3">
          {/* Header */}
          <div className="space-y-1">
            <h3 className="font-bold text-lg group-hover:text-primary transition-colors line-clamp-1">
              {ride.name}
            </h3>
            
            {showParkName && ride.park?.name && (
              <div className="flex items-center text-sm text-muted-foreground">
                <MapPin className="w-3 h-3 mr-1" />
                {ride.park.name}
                {ride.park.location?.city && `, ${ride.park.location.city}`}
              </div>
            )}
          </div>

          {/* Description */}
          {ride.description && (
            <p className="text-sm text-muted-foreground line-clamp-2">
              {ride.description}
            </p>
          )}

          {/* Category Badge */}
          <Badge variant="outline" className="text-xs">
            {formatCategory(ride.category)}
          </Badge>

          {/* Stats */}
          <div className="flex items-center justify-between text-sm">
            <div className="flex items-center gap-4">
              {Number(ride.max_speed_kmh) > 0 && (
                <div className="flex items-center gap-1">
                  <Zap className="w-3 h-3 text-primary" />
                  <span className="text-primary font-medium">
                    <MeasurementDisplay value={ride.max_speed_kmh} type="speed" />
                  </span>
                </div>
              )}
              {Number(ride.max_height_meters) > 0 && (
                <div className="flex items-center gap-1">
                  <span className="text-accent font-medium">
                    <MeasurementDisplay value={ride.max_height_meters} type="distance" />
                  </span>
                </div>
              )}
              {Number(ride.duration_seconds) > 0 && (
                <div className="flex items-center gap-1">
                  <Clock className="w-3 h-3 text-secondary" />
                  <span className="text-secondary font-medium">{Math.floor(ride.duration_seconds / 60)}</span>
                  <span className="text-muted-foreground text-xs">min</span>
                </div>
              )}
            </div>
            
            {ride.average_rating > 0 && (
              <div className="flex items-center gap-1">
                <Star className="w-3 h-3 fill-yellow-400 text-yellow-400" />
                <span className="text-sm font-medium">{ride.average_rating.toFixed(1)}</span>
                {ride.review_count > 0 && (
                  <span className="text-xs text-muted-foreground">({ride.review_count})</span>
                )}
              </div>
            )}
          </div>
        </CardContent>
      </div>
    </Card>
  );
}