'use client' import { Container, Grid, Card, CardContent, Typography, Box, TextField, Button, Paper, List, ListItem, ListItemText, Chip, InputAdornment, FormControl, InputLabel, Select, MenuItem, Accordion, AccordionSummary, AccordionDetails, useTheme, CircularProgress, Skeleton, } from '@mui/material' import { Search, FilterList, ExpandMore, MenuBook, Close, History, } from '@mui/icons-material' import { useState, useEffect } from 'react' import { useTranslations, useLocale } from 'next-intl' import { translateBookName } from '@/lib/book-translations' interface SearchResult { id: string book: string chapter: number verse: number text: string relevance: number } interface SearchFilter { testament: 'all' | 'old' | 'new' books: string[] exactMatch: boolean } export default function SearchPage() { const theme = useTheme() const t = useTranslations('pages.search') const locale = useLocale() const [searchQuery, setSearchQuery] = useState('') const [results, setResults] = useState([]) const [loading, setLoading] = useState(false) const [searchHistory, setSearchHistory] = useState([]) const [filters, setFilters] = useState({ testament: 'all', books: [], exactMatch: false, }) const oldTestamentBooks = [ 'Geneza', 'Exodul', 'Leviticul', 'Numerii', 'Deuteronomul', 'Iosua', 'Judecătorii', 'Rut', '1 Samuel', '2 Samuel', 'Psalmii', 'Proverbele', 'Isaia', 'Ieremia', 'Daniel' ] const newTestamentBooks = [ 'Matei', 'Marcu', 'Luca', 'Ioan', 'Faptele Apostolilor', 'Romani', '1 Corinteni', '2 Corinteni', 'Galateni', 'Efeseni', 'Filipeni', 'Coloseni', 'Evrei', 'Iacob', '1 Petru', 'Apocalipsa' ] const popularSearches: string[] = t.raw('popular.items') useEffect(() => { // Load search history from localStorage const saved = localStorage.getItem('searchHistory') if (saved) { setSearchHistory(JSON.parse(saved)) } }, []) const handleSearch = async () => { if (!searchQuery.trim()) return setLoading(true) // Add to search history const newHistory = [searchQuery, ...searchHistory.filter(s => s !== searchQuery)].slice(0, 10) setSearchHistory(newHistory) localStorage.setItem('searchHistory', JSON.stringify(newHistory)) try { const params = new URLSearchParams({ q: searchQuery, testament: filters.testament, exactMatch: filters.exactMatch.toString(), books: filters.books.join(','), }) const response = await fetch(`/api/search/verses?${params}`) if (!response.ok) { throw new Error('Search failed') } const data = await response.json() setResults(data.results || []) } catch (error) { console.error('Error searching:', error) // Mock results for demo setResults([ { id: '1', book: 'Ioan', chapter: 3, verse: 16, text: 'Fiindcă atât de mult a iubit Dumnezeu lumea, că a dat pe singurul Său Fiu, pentru ca oricine crede în El să nu piară, ci să aibă viața veșnică.', relevance: 0.95, }, { id: '2', book: '1 Corinteni', chapter: 13, verse: 4, text: 'Dragostea este îndelung răbdătoare, dragostea este binevoitoare; dragostea nu pizmuiește...', relevance: 0.89, }, ]) } finally { setLoading(false) } } const handleKeyPress = (event: React.KeyboardEvent) => { if (event.key === 'Enter') { handleSearch() } } const clearFilters = () => { setFilters({ testament: 'all', books: [], exactMatch: false, }) } const highlightSearchTerm = (text: string, query: string) => { if (!query) return text const regex = new RegExp(`(${query})`, 'gi') const parts = text.split(regex) return parts.map((part, index) => regex.test(part) ? ( {part} ) : ( part ) ) } return ( {/* Header */} {t('title')} {t('subtitle')} {/* Search Sidebar */} {/* Search Filters */} {t('filters.title')} {t('filters.testament')} }> {t('filters.specificBooks')} {(filters.testament === 'old' || filters.testament === 'all' ? oldTestamentBooks : []) .concat(filters.testament === 'new' || filters.testament === 'all' ? newTestamentBooks : []) .map((book) => ( { const newBooks = filters.books.includes(book) ? filters.books.filter(b => b !== book) : [...filters.books, book] setFilters({ ...filters, books: newBooks }) }} sx={{ mb: 0.5, mr: 0.5 }} /> ))} {/* Search History */} {searchHistory.length > 0 && ( {t('history.title')} {searchHistory.slice(0, 5).map((query, index) => ( setSearchQuery(query)} sx={{ mb: 0.5, mr: 0.5 }} /> ))} )} {/* Popular Searches */} {t('popular.title')} {popularSearches.map((query, index) => ( setSearchQuery(query)} sx={{ mb: 0.5, mr: 0.5 }} /> ))} {/* Main Search Area */} {/* Search Input */} setSearchQuery(e.target.value)} onKeyPress={handleKeyPress} InputProps={{ startAdornment: ( ), endAdornment: searchQuery && ( ), }} /> {filters.books.length > 0 && ( {t('searchIn', { books: filters.books.map(b => translateBookName(b, locale)).join(', ') })} )} {/* Search Results */} {loading && searchQuery && ( {t('searching')} {Array.from({ length: 3 }).map((_, index) => ( } secondary={ } /> ))} )} {results.length > 0 && !loading && ( {t('results', { count: results.length })} {results.map((result) => ( {result.book} {result.chapter}:{result.verse} } secondary={ {highlightSearchTerm(result.text, searchQuery)} } /> ))} )} {!loading && searchQuery && results.length === 0 && ( {t('noResults.title')} {t('noResults.description')} )} {!searchQuery && !loading && ( {t('empty.title')} {t('empty.description')} )} ) }