import { useState, useEffect, lazy, Suspense } from 'react';
import { useParams, useNavigate } from 'react-router-dom';
import { Header } from '@/components/layout/Header';
import { trackPageView } from '@/lib/viewTracking';
import { getBannerUrls } from '@/lib/cloudflareImageUtils';
import { Button } from '@/components/ui/button';
import { Badge } from '@/components/ui/badge';
import { Card, CardContent } from '@/components/ui/card';
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs';
import { Dialog, DialogContent } from '@/components/ui/dialog';
import { AdminFormSkeleton } from '@/components/loading/PageSkeletons';
import { ArrowLeft, MapPin, Star, Globe, Calendar, Edit, FerrisWheel, Gauge } from 'lucide-react';
import { Company, Park } from '@/types/database';
import { supabase } from '@/integrations/supabase/client';
import { OperatorPhotoGallery } from '@/components/companies/OperatorPhotoGallery';
import { ParkCard } from '@/components/parks/ParkCard';

// Lazy load admin form
const OperatorForm = lazy(() => import('@/components/admin/OperatorForm').then(m => ({ default: m.OperatorForm })));
import { useAuth } from '@/hooks/useAuth';
import { useUserRole } from '@/hooks/useUserRole';
import { toast } from '@/hooks/use-toast';
import { getErrorMessage } from '@/lib/errorHandler';
import { submitCompanyUpdate } from '@/lib/companyHelpers';
import { VersionIndicator } from '@/components/versioning/VersionIndicator';
import { EntityHistoryTabs } from '@/components/history/EntityHistoryTabs';
import { useAuthModal } from '@/hooks/useAuthModal';
import { useDocumentTitle } from '@/hooks/useDocumentTitle';
import { useOpenGraph } from '@/hooks/useOpenGraph';

export default function OperatorDetail() {
  const { slug } = useParams<{ slug: string }>();
  const navigate = useNavigate();
  const [operator, setOperator] = useState<Company | null>(null);
  const [parks, setParks] = useState<Park[]>([]);
  const [loading, setLoading] = useState(true);
  const [parksLoading, setParksLoading] = useState(true);
  const [isEditModalOpen, setIsEditModalOpen] = useState(false);
  const [totalParks, setTotalParks] = useState<number>(0);
  const [operatingRides, setOperatingRides] = useState<number>(0);
  const [statsLoading, setStatsLoading] = useState(true);
  const [totalPhotos, setTotalPhotos] = useState<number>(0);
  const { user } = useAuth();
  const { isModerator } = useUserRole();
  const { requireAuth } = useAuthModal();
  
  // Update document title when operator changes
  useDocumentTitle(operator?.name || 'Operator Details');
  
  // Update Open Graph meta tags
  useOpenGraph({
    title: operator?.name || '',
    description: operator?.description ?? (operator ? `${operator.name} - Park Operator${operator.headquarters_location ? ` based in ${operator.headquarters_location}` : ''}` : undefined),
    imageUrl: operator?.banner_image_url ?? undefined,
    imageId: operator?.banner_image_id ?? undefined,
    type: 'profile',
    enabled: !!operator
  });

  useEffect(() => {
    if (slug) {
      fetchOperatorData();
    }
  }, [slug]);

  // Track page view when operator is loaded
  useEffect(() => {
    if (operator?.id) {
      trackPageView('company', operator.id);
    }
  }, [operator?.id]);

  const fetchOperatorData = async () => {
    try {
      const { data, error } = await supabase
        .from('companies')
        .select('*')
        .eq('slug', slug || '')
        .eq('company_type', 'operator')
        .maybeSingle();

      if (error) throw error;
      setOperator(data);
      
      // Fetch parks operated by this operator
      if (data) {
        fetchParks(data.id);
        fetchStatistics(data.id);
        fetchPhotoCount(data.id);
      }
    } catch (error) {
      console.error('Error fetching operator:', error);
    } finally {
      setLoading(false);
    }
  };

  const fetchParks = async (operatorId: string) => {
    try {
      const { data, error } = await supabase
        .from('parks')
        .select(`
          *,
          location:locations(*)
        `)
        .eq('operator_id', operatorId)
        .order('name')
        .limit(6);

      if (error) throw error;
      setParks(data || []);
    } catch (error) {
      console.error('Error fetching parks:', error);
    } finally {
      setParksLoading(false);
    }
  };

  const fetchStatistics = async (operatorId: string) => {
    try {
      // Get total parks count
      const { count: parksCount, error: parksError } = await supabase
        .from('parks')
        .select('id', { count: 'exact', head: true })
        .eq('operator_id', operatorId);

      if (parksError) throw parksError;
      setTotalParks(parksCount || 0);

      // Get operating rides count across all parks
      const { data: ridesData, error: ridesError } = await supabase
        .from('rides')
        .select('id, parks!inner(operator_id)')
        .eq('parks.operator_id', operatorId)
        .eq('status', 'operating');

      if (ridesError) throw ridesError;
      setOperatingRides(ridesData?.length || 0);
    } catch (error) {
      console.error('Error fetching statistics:', error);
    } finally {
      setStatsLoading(false);
    }
  };

  const fetchPhotoCount = async (operatorId: string) => {
    try {
      const { count, error } = await supabase
        .from('photos')
        .select('id', { count: 'exact', head: true })
        .eq('entity_type', 'operator')
        .eq('entity_id', operatorId);

      if (error) throw error;
      setTotalPhotos(count || 0);
    } catch (error) {
      console.error('Error fetching photo count:', error);
      setTotalPhotos(0);
    }
  };

  const handleEditSubmit = async (data: any) => {
    try {
      await submitCompanyUpdate(
        operator!.id,
        data,
        user!.id
      );

      toast({
        title: "Edit Submitted",
        description: "Your edit has been submitted for review."
      });

      setIsEditModalOpen(false);
    } catch (error) {
      const errorMsg = getErrorMessage(error);
      toast({
        title: "Error",
        description: errorMsg,
        variant: "destructive"
      });
    }
  };

  if (loading) {
    return (
      <div className="min-h-screen bg-background">
        <Header />
        <div className="container mx-auto px-4 py-8">
          <div className="animate-pulse space-y-6">
            <div className="h-64 bg-muted rounded-lg"></div>
            <div className="h-8 bg-muted rounded w-1/2"></div>
          </div>
        </div>
      </div>
    );
  }

  if (!operator) {
    return (
      <div className="min-h-screen bg-background">
        <Header />
        <div className="container mx-auto px-4 py-8">
          <div className="text-center py-12">
            <h1 className="text-2xl font-bold mb-4">Operator Not Found</h1>
            <Button onClick={() => navigate('/operators')}>
              <ArrowLeft className="w-4 h-4 mr-2" />
              Back to Operators
            </Button>
          </div>
        </div>
      </div>
    );
  }

  return (
    <div className="min-h-screen bg-background">
      <Header />
      
      <main className="container mx-auto px-4 py-8 max-w-7xl">
        {/* Back Button and Edit Button */}
        <div className="flex items-center justify-between mb-6">
          <Button variant="ghost" onClick={() => navigate('/operators')}>
            <ArrowLeft className="w-4 h-4 mr-2" />
            Back to Operators
          </Button>
          
          <Button 
            variant="outline"
            onClick={() => requireAuth(() => setIsEditModalOpen(true), "Sign in to edit this operator")}
          >
            <Edit className="w-4 h-4 mr-2" />
            Edit Operator
          </Button>
        </div>

        {/* Hero Section */}
        <div className="relative mb-8">
          <div className="aspect-[21/9] bg-gradient-to-br from-primary/20 via-secondary/20 to-accent/20 rounded-lg overflow-hidden relative">
            {(operator.banner_image_url || operator.banner_image_id) ? (
              <picture>
                <source 
                  media="(max-width: 768px)" 
                  srcSet={getBannerUrls(operator.banner_image_id ?? undefined).mobile ?? operator.banner_image_url ?? undefined} 
                />
                <img 
                  src={getBannerUrls(operator.banner_image_id ?? undefined).desktop ?? operator.banner_image_url ?? undefined} 
                  alt={operator.name}
                  className="w-full h-full object-cover"
                  loading="eager"
                />
              </picture>
            ) : operator.logo_url ? (
              <div className="flex items-center justify-center h-full bg-background/90">
                <img 
                  src={operator.logo_url} 
                  alt={operator.name}
                  className="max-h-48 object-contain"
                />
              </div>
            ) : (
              <div className="flex items-center justify-center h-full">
                <FerrisWheel className="w-24 h-24 opacity-50" />
              </div>
            )}
            
            <div className="absolute bottom-0 left-0 right-0 p-8 bg-gradient-to-t from-black/60 to-transparent">
              <div className="flex items-end justify-between">
                <div>
                  <Badge variant="outline" className="bg-black/20 text-white border-white/20 mb-2">
                    Operator
                  </Badge>
                  <h1 className="text-4xl md:text-6xl font-bold text-white mb-2">
                    {operator.name}
                  </h1>
                  {operator.headquarters_location && (
                    <div className="flex items-center text-white/90 text-lg">
                      <MapPin className="w-5 h-5 mr-2" />
                      {operator.headquarters_location}
                    </div>
                  )}
                  <div className="mt-3">
                    <VersionIndicator 
                      entityType="company" 
                      entityId={operator.id} 
                      entityName={operator.name}
                    />
                  </div>
                </div>
                
                {(operator.average_rating ?? 0) > 0 && (
                  <div className="bg-black/30 backdrop-blur-md rounded-lg p-6 text-center">
                    <div className="flex items-center gap-2 text-white mb-2">
                      <Star className="w-6 h-6 fill-yellow-400 text-yellow-400" />
                      <span className="text-3xl font-bold">
                        {(operator.average_rating ?? 0).toFixed(1)}
                      </span>
                    </div>
                    <div className="text-white/90 text-sm">
                      {operator.review_count} {operator.review_count === 1 ? "review" : "reviews"}
                    </div>
                  </div>
                )}
              </div>
            </div>
          </div>
        </div>

        {/* Company Info */}
        <div className="flex flex-wrap justify-center gap-4 mb-8">
          {!statsLoading && totalParks > 0 && (
            <Card>
              <CardContent className="p-4 text-center">
                <FerrisWheel className="w-6 h-6 text-primary mx-auto mb-2" />
                <div className="text-2xl font-bold">{totalParks}</div>
                <div className="text-sm text-muted-foreground">
                  {totalParks === 1 ? 'Park Operated' : 'Parks Operated'}
                </div>
              </CardContent>
            </Card>
          )}
          
          {!statsLoading && operatingRides > 0 && (
            <Card>
              <CardContent className="p-4 text-center">
                <Gauge className="w-6 h-6 text-accent mx-auto mb-2" />
                <div className="text-2xl font-bold">{operatingRides}</div>
                <div className="text-sm text-muted-foreground">
                  Operating {operatingRides === 1 ? 'Ride' : 'Rides'}
                </div>
              </CardContent>
            </Card>
          )}
          
          {operator.founded_year && (
            <Card>
              <CardContent className="p-4 text-center">
                <Calendar className="w-6 h-6 text-primary mx-auto mb-2" />
                <div className="text-2xl font-bold">{operator.founded_year}</div>
                <div className="text-sm text-muted-foreground">Founded</div>
              </CardContent>
            </Card>
          )}
          
          {operator.website_url && (
            <Card>
              <CardContent className="p-4 text-center">
                <Globe className="w-6 h-6 text-primary mx-auto mb-2" />
                <a 
                  href={operator.website_url} 
                  target="_blank" 
                  rel="noopener noreferrer"
                  className="text-sm text-primary hover:underline break-all"
                >
                  Visit Website
                </a>
              </CardContent>
            </Card>
          )}
        </div>

        {/* Tabs */}
        <Tabs defaultValue="overview" className="w-full">
          <TabsList className="grid w-full grid-cols-2 md:grid-cols-4">
            <TabsTrigger value="overview">Overview</TabsTrigger>
            <TabsTrigger value="parks">
              Parks {!statsLoading && totalParks > 0 && `(${totalParks})`}
            </TabsTrigger>
            <TabsTrigger value="photos">
              Photos {!statsLoading && totalPhotos > 0 && `(${totalPhotos})`}
            </TabsTrigger>
            <TabsTrigger value="history">History</TabsTrigger>
          </TabsList>

          <TabsContent value="overview" className="space-y-6">
            {operator.description && (
              <Card>
                <CardContent className="p-6">
                  <h2 className="text-2xl font-bold mb-4">About</h2>
                  <p className="text-muted-foreground whitespace-pre-wrap">
                    {operator.description}
                  </p>
                </CardContent>
              </Card>
            )}
          </TabsContent>

          <TabsContent value="parks">
            <Card>
              <CardContent className="p-6">
                <div className="flex items-center justify-between mb-6">
                  <h2 className="text-2xl font-bold">Parks Operated</h2>
                  <Button 
                    variant="outline" 
                    onClick={() => navigate(`/operators/${operator.slug}/parks`)}
                  >
                    View All Parks
                  </Button>
                </div>
                
                {parksLoading ? (
                  <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
                    {[1, 2, 3].map((i) => (
                      <div key={i} className="h-64 bg-muted rounded-lg animate-pulse" />
                    ))}
                  </div>
                ) : parks.length > 0 ? (
                  <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 xl:grid-cols-5 2xl:grid-cols-6 3xl:grid-cols-7 gap-4 lg:gap-5 xl:gap-4 2xl:gap-5">
                    {parks.map((park) => (
                      <ParkCard key={park.id} park={park} />
                    ))}
                  </div>
                ) : (
                  <div className="text-center py-12 text-muted-foreground">
                    <FerrisWheel className="w-12 h-12 mx-auto mb-4 opacity-50" />
                    <p>No parks found for {operator.name}</p>
                  </div>
                )}
              </CardContent>
            </Card>
          </TabsContent>

          <TabsContent value="photos">
            <OperatorPhotoGallery 
              operatorId={operator.id}
              operatorName={operator.name}
            />
          </TabsContent>

          <TabsContent value="history" className="mt-6">
            <EntityHistoryTabs
              entityType="company"
              entityId={operator.id}
              entityName={operator.name}
            />
          </TabsContent>
        </Tabs>
      </main>

      {/* Edit Modal */}
      <Dialog open={isEditModalOpen} onOpenChange={setIsEditModalOpen}>
        <DialogContent className="max-w-4xl max-h-[90vh] overflow-y-auto">
          <Suspense fallback={<AdminFormSkeleton />}>
            <OperatorForm
              initialData={{
                id: operator.id,
                name: operator.name,
                slug: operator.slug,
                description: operator.description ?? undefined,
              company_type: 'operator',
              person_type: (operator.person_type || 'company') as 'company' | 'individual' | 'firm' | 'organization',
              website_url: operator.website_url ?? undefined,
                founded_year: operator.founded_year ?? undefined,
                headquarters_location: operator.headquarters_location ?? undefined,
                banner_image_url: operator.banner_image_url ?? undefined,
                card_image_url: operator.card_image_url ?? undefined
              }}
              onSubmit={handleEditSubmit}
              onCancel={() => setIsEditModalOpen(false)}
            />
          </Suspense>
        </DialogContent>
      </Dialog>
    </div>
  );
}
