import React, { useState } from 'react'; import { useQuery } from '@tanstack/react-query'; import { useNavigate } from 'react-router-dom'; import { Header } from '@/components/layout/Header'; import { Input } from '@/components/ui/input'; import { Button } from '@/components/ui/button'; import { Badge } from '@/components/ui/badge'; import { Dialog, DialogContent } from '@/components/ui/dialog'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'; import { Search, Filter, Building2, Plus } from 'lucide-react'; import { supabase } from '@/lib/supabaseClient'; import ParkOwnerCard from '@/components/park-owners/ParkOwnerCard'; import { PropertyOwnerForm } from '@/components/admin/PropertyOwnerForm'; import { Company } from '@/types/database'; import { useAuth } from '@/hooks/useAuth'; import { useUserRole } from '@/hooks/useUserRole'; import { toast } from '@/hooks/use-toast'; import { submitCompanyCreation } from '@/lib/companyHelpers'; import { useAuthModal } from '@/hooks/useAuthModal'; import { getErrorMessage } from '@/lib/errorHandler'; import { useDocumentTitle } from '@/hooks/useDocumentTitle'; import { useOpenGraph } from '@/hooks/useOpenGraph'; import { SubmissionErrorBoundary } from '@/components/error/SubmissionErrorBoundary'; const ParkOwners = () => { useDocumentTitle('Property Owners'); const navigate = useNavigate(); const { user } = useAuth(); const { isModerator } = useUserRole(); const { requireAuth } = useAuthModal(); const [searchTerm, setSearchTerm] = useState(''); const [sortBy, setSortBy] = useState('name'); const [filterBy, setFilterBy] = useState('all'); const [isCreateModalOpen, setIsCreateModalOpen] = useState(false); const { data: parkOwners, isLoading } = useQuery({ queryKey: ['park-owners'], queryFn: async () => { // Get companies that are property owners with park counts const { data, error } = await supabase .from('companies') .select(` *, parks:parks!property_owner_id(count) `) .in('id', await supabase .from('parks') .select('property_owner_id') .not('property_owner_id', 'is', null) .then(({ data }) => data?.map(park => park.property_owner_id).filter((id): id is string => id !== null) || []) ) .order('name'); if (error) throw error; // Transform the data to include park_count const transformedData = data?.map(company => ({ ...company, park_count: company.parks?.[0]?.count || 0 })) || []; return transformedData as (Company & { park_count: number })[]; }, }); const handleCreateSubmit = async (data: any) => { try { await submitCompanyCreation( data, 'property_owner', user!.id ); toast({ title: "Property Owner Submitted", description: "Your submission has been sent for review." }); setIsCreateModalOpen(false); } catch (error) { const errorMsg = getErrorMessage(error); toast({ title: "Error", description: errorMsg, variant: "destructive" }); } }; const filteredAndSortedOwners = React.useMemo(() => { if (!parkOwners) return []; let filtered = parkOwners.filter(owner => { const matchesSearch = owner.name.toLowerCase().includes(searchTerm.toLowerCase()) || (owner.description && owner.description.toLowerCase().includes(searchTerm.toLowerCase())); if (filterBy === 'all') return matchesSearch; if (filterBy === 'with-rating') return matchesSearch && (owner.average_rating ?? 0) > 0; if (filterBy === 'established') return matchesSearch && owner.founded_year; return matchesSearch; }); // Sort filtered.sort((a, b) => { switch (sortBy) { case 'name': return a.name.localeCompare(b.name); case 'rating': return (b.average_rating || 0) - (a.average_rating || 0); case 'founded': return (b.founded_year || 0) - (a.founded_year || 0); case 'reviews': return (b.review_count || 0) - (a.review_count || 0); default: return 0; } }); return filtered; }, [parkOwners, searchTerm, sortBy, filterBy]); useOpenGraph({ title: 'Property Owners - ThrillWiki', description: `Browse ${filteredAndSortedOwners.length} theme park property owners worldwide`, imageUrl: filteredAndSortedOwners[0]?.banner_image_url ?? undefined, imageId: filteredAndSortedOwners[0]?.banner_image_id ?? undefined, type: 'website', enabled: !isLoading }); return (
{/* Page Header */}

Property Owners

Discover companies that own and manage theme parks

{filteredAndSortedOwners?.length || 0} owners {searchTerm && ( Searching: "{searchTerm}" )}
{/* Search and Filters */}
setSearchTerm(e.target.value)} className="pl-10 h-11" />
{/* Loading State */} {isLoading && (

Loading property owners...

)} {/* Property Owners Grid */} {!isLoading && filteredAndSortedOwners && (
{filteredAndSortedOwners.map((owner) => ( ))}
)} {/* Empty State */} {!isLoading && filteredAndSortedOwners?.length === 0 && (

No property owners found

{searchTerm ? "Try adjusting your search terms or filters" : "No property owners are currently available" }

)} {/* Create Modal */} setIsCreateModalOpen(false)} />
); }; export default ParkOwners;