import React, { useState, useEffect } from 'react'; import { useSearchParams } from 'react-router-dom'; import { useToast } from '@/hooks/use-toast'; import { searchAPI } from '../lib/search-api'; import PageHeader from '../components/PageHeader'; import ContentGrid from '../components/ContentGrid'; import ContentCard from '../components/ContentCard'; import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; import { Loader2 } from 'lucide-react'; const SearchPage = () => { const [searchParams] = useSearchParams(); const query = searchParams.get('q') || ''; const [searchResults, setSearchResults] = useState<{ movies: string[]; shows: string[]; episodes: { series: string; title: string; path: string; season: string; }[]; }>({ movies: [], shows: [], episodes: [] }); const [loading, setLoading] = useState(false); const [activeTab, setActiveTab] = useState('all'); const { toast } = useToast(); useEffect(() => { const fetchSearchResults = async () => { if (!query) return; try { setLoading(true); const results = await searchAPI.search(query); setSearchResults({ movies: results.films || [], shows: results.series || [], episodes: results.episodes || [] }); } catch (error) { console.error('Search error:', error); toast({ title: "Search Failed", description: "Unable to perform search. Please try again.", variant: "destructive" }); } finally { setLoading(false); } }; fetchSearchResults(); }, [query, toast]); const getTotalResultsCount = () => { return searchResults.movies.length + searchResults.shows.length + searchResults.episodes.length; }; const renderMovieResults = () => { if (searchResults.movies.length === 0) { return

No movies found matching "{query}"

; } return (
{searchResults.movies.map((title, index) => ( ))}
); }; const renderShowResults = () => { if (searchResults.shows.length === 0) { return

No TV shows found matching "{query}"

; } return (
{searchResults.shows.map((title, index) => ( ))}
); }; const renderEpisodeResults = () => { if (searchResults.episodes.length === 0) { return

No episodes found matching "{query}"

; } return (
{searchResults.episodes.map((episode, index) => (

{episode.title}

{episode.series} • Season {episode.season}

))}
); }; return (
{loading ? (
) : query ? ( All Results ({getTotalResultsCount()}) Movies ({searchResults.movies.length}) TV Shows ({searchResults.shows.length}) Episodes ({searchResults.episodes.length}) {getTotalResultsCount() === 0 ? (

No results found matching "{query}"

) : ( <> {searchResults.movies.length > 0 && (

Movies

{renderMovieResults()}
)} {searchResults.shows.length > 0 && (

TV Shows

{renderShowResults()}
)} {searchResults.episodes.length > 0 && (

Episodes

{renderEpisodeResults()}
)} )}
{renderMovieResults()} {renderShowResults()} {renderEpisodeResults()}
) : (

Enter a search term to find movies, TV shows, and episodes.

)}
); }; export default SearchPage;