import { useState, useEffect, lazy, Suspense } from 'react';
import { useParams, useNavigate } from 'react-router-dom';
import { Header } from '@/components/layout/Header';
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, Ruler } from 'lucide-react';
import { Company } from '@/types/database';
import { supabase } from '@/lib/supabaseClient';
import { DesignerPhotoGallery } from '@/components/companies/DesignerPhotoGallery';
import { handleNonCriticalError } from '@/lib/errorHandler';

// Lazy load admin form
const DesignerForm = lazy(() => import('@/components/admin/DesignerForm').then(m => ({ default: m.DesignerForm })));
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 { trackPageView } from '@/lib/viewTracking';
import { useAuthModal } from '@/hooks/useAuthModal';
import { useDocumentTitle } from '@/hooks/useDocumentTitle';
import { useOpenGraph } from '@/hooks/useOpenGraph';

export default function DesignerDetail() {
  const { slug } = useParams<{ slug: string }>();
  const navigate = useNavigate();
  const [designer, setDesigner] = useState<Company | null>(null);
  const [loading, setLoading] = useState(true);
  const [isEditModalOpen, setIsEditModalOpen] = useState(false);
  const [totalRides, setTotalRides] = useState<number>(0);
  const [totalPhotos, setTotalPhotos] = useState<number>(0);
  const [statsLoading, setStatsLoading] = useState(true);
  const { user } = useAuth();
  const { isModerator } = useUserRole();
  const { requireAuth } = useAuthModal();
  
  // Update document title when designer changes
  useDocumentTitle(designer?.name || 'Designer Details');
  
  // Update Open Graph meta tags
  useOpenGraph({
    title: designer?.name || '',
    description: designer?.description ?? (designer ? `${designer.name} - Ride Designer${designer.headquarters_location ? ` based in ${designer.headquarters_location}` : ''}` : ''),
    imageUrl: designer?.banner_image_url ?? undefined,
    imageId: designer?.banner_image_id ?? undefined,
    type: 'profile',
    enabled: !!designer
  });

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

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

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

      if (error) throw error;
      setDesigner(data);
      if (data) {
        fetchStatistics(data.id);
      }
    } catch (error) {
      handleNonCriticalError(error, {
        action: 'Fetch designer',
        metadata: { slug }
      });
    } finally {
      setLoading(false);
    }
  };

  const fetchStatistics = async (designerId: string) => {
    try {
      // Count rides
      const { count: ridesCount, error: ridesError } = await supabase
        .from('rides')
        .select('id', { count: 'exact', head: true })
        .eq('designer_id', designerId);

      if (ridesError) throw ridesError;
      setTotalRides(ridesCount || 0);

      // Count photos
      const { count: photosCount, error: photosError } = await supabase
        .from('photos')
        .select('id', { count: 'exact', head: true })
        .eq('entity_type', 'designer')
        .eq('entity_id', designerId);

      if (photosError) throw photosError;
      setTotalPhotos(photosCount || 0);
    } catch (error) {
      handleNonCriticalError(error, {
        action: 'Fetch designer statistics',
        metadata: { designerId }
      });
    } finally {
      setStatsLoading(false);
    }
  };

  const handleEditSubmit = async (data: any) => {
    try {
      await submitCompanyUpdate(
        designer!.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 (!designer) {
    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">Designer Not Found</h1>
            <Button onClick={() => navigate('/designers')}>
              <ArrowLeft className="w-4 h-4 mr-2" />
              Back to Designers
            </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('/designers')}>
            <ArrowLeft className="w-4 h-4 mr-2" />
            Back to Designers
          </Button>
          
          <Button 
            variant="outline"
            onClick={() => requireAuth(() => setIsEditModalOpen(true), "Sign in to edit this designer")}
          >
            <Edit className="w-4 h-4 mr-2" />
            Edit Designer
          </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">
            {(designer.banner_image_url || designer.banner_image_id) ? (
              <picture>
                <source 
                  media="(max-width: 768px)" 
                  srcSet={(getBannerUrls(designer.banner_image_id ?? undefined).mobile || designer.banner_image_url) ?? undefined} 
                />
                <img 
                  src={(getBannerUrls(designer.banner_image_id ?? undefined).desktop || designer.banner_image_url) ?? undefined} 
                  alt={designer.name}
                  className="w-full h-full object-cover"
                  loading="eager"
                />
              </picture>
            ) : designer.logo_url ? (
              <div className="flex items-center justify-center h-full bg-background/90">
                <img 
                  src={designer.logo_url} 
                  alt={designer.name}
                  className="max-h-48 object-contain"
                />
              </div>
            ) : (
              <div className="flex items-center justify-center h-full">
                <Ruler 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">
                    Designer
                  </Badge>
                  <h1 className="text-4xl md:text-6xl font-bold text-white mb-2">
                    {designer.name}
                  </h1>
                  {designer.headquarters_location && (
                    <div className="flex items-center text-white/90 text-lg">
                      <MapPin className="w-5 h-5 mr-2" />
                      {designer.headquarters_location}
                    </div>
                  )}
                  <div className="mt-3">
                    <VersionIndicator 
                      entityType="company" 
                      entityId={designer.id} 
                      entityName={designer.name}
                    />
                  </div>
                </div>
                
                {(designer.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">
                        {(designer.average_rating ?? 0).toFixed(1)}
                      </span>
                    </div>
                    <div className="text-white/90 text-sm">
                      {designer.review_count} {designer.review_count === 1 ? "review" : "reviews"}
                    </div>
                  </div>
                )}
              </div>
            </div>
          </div>
        </div>

        {/* Company Info */}
        <div className="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 gap-4 mb-8 max-w-4xl mx-auto">
          {designer.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">{designer.founded_year}</div>
                <div className="text-sm text-muted-foreground">Founded</div>
              </CardContent>
            </Card>
          )}
          {designer.website_url && (
            <Card>
              <CardContent className="p-4 text-center">
                <Globe className="w-6 h-6 text-primary mx-auto mb-2" />
                <a 
                  href={designer.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="rides">
              Rides {!statsLoading && totalRides > 0 && `(${totalRides})`}
            </TabsTrigger>
            <TabsTrigger value="photos">
              Photos {!statsLoading && totalPhotos > 0 && `(${totalPhotos})`}
            </TabsTrigger>
            <TabsTrigger value="history">History</TabsTrigger>
          </TabsList>

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

          <TabsContent value="rides">
            <Card>
              <CardContent className="p-6">
                <div className="flex items-center justify-between mb-4">
                  <h2 className="text-2xl font-bold">Rides</h2>
                  <Button 
                    variant="outline" 
                    onClick={() => navigate(`/designers/${designer.slug}/rides`)}
                  >
                    View All Rides
                  </Button>
                </div>
                <p className="text-muted-foreground">
                  View all rides designed by {designer.name}
                </p>
              </CardContent>
            </Card>
          </TabsContent>

          <TabsContent value="photos">
            <DesignerPhotoGallery 
              designerId={designer.id}
              designerName={designer.name}
            />
          </TabsContent>

          <TabsContent value="history" className="mt-6">
            <EntityHistoryTabs
              entityType="company"
              entityId={designer.id}
              entityName={designer.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 />}>
            <DesignerForm
              initialData={{
                id: designer.id,
                name: designer.name,
                slug: designer.slug,
                description: designer.description ?? undefined,
              company_type: 'designer',
              person_type: (designer.person_type || 'company') as 'company' | 'individual' | 'firm' | 'organization',
              website_url: designer.website_url ?? undefined,
                founded_year: designer.founded_year ?? undefined,
                headquarters_location: designer.headquarters_location ?? undefined,
                banner_image_url: designer.banner_image_url ?? undefined,
                card_image_url: designer.card_image_url ?? undefined
              }}
              onSubmit={handleEditSubmit}
              onCancel={() => setIsEditModalOpen(false)}
            />
          </Suspense>
        </DialogContent>
      </Dialog>
    </div>
  );
}
