import { useState } from 'react' import { Box, Button, Card, Stack, Typography } from '@mui/material' import { useNavigate } from 'react-router' import { useQuery } from '@tanstack/react-query' import { useUnifiedResults } from '../../hooks/useUnifiedResults' import { useCompareStore } from '../../stores/compareStore' import { needService } from '../../services/needService' import { FeedEmptyState, FeedSkeleton, ResultFeedHeader, ResultFilterBar, UnifiedResultFeed, } from '../../components/results' import type { ResultType } from '../../domain/enums' import type { UnifiedMatchResult } from '../../domain/unifiedResult' type FilterSource = ResultType | 'ALL' type SortBy = 'score' | 'rent' | 'area' function sortResults(results: UnifiedMatchResult[], sortBy: SortBy): UnifiedMatchResult[] { return [...results].sort((a, b) => { if (sortBy === 'score') return b.matchScore - a.matchScore const propA = a.resultType !== 'FUTURE_AVAILABILITY' ? (a as { property: { rentPricePerSqm: number; areaSqm: number } }).property : null const propB = b.resultType !== 'FUTURE_AVAILABILITY' ? (b as { property: { rentPricePerSqm: number; areaSqm: number } }).property : null if (sortBy === 'rent') return (propA?.rentPricePerSqm ?? 0) - (propB?.rentPricePerSqm ?? 0) if (sortBy === 'area') return (propB?.areaSqm ?? 0) - (propA?.areaSqm ?? 0) return 0 }) } export default function Results() { const navigate = useNavigate() const [filterSource, setFilterSource] = useState('ALL') const [sortBy, setSortBy] = useState('score') const { data: results = [], isLoading } = useUnifiedResults() const { data: needResp } = useQuery({ queryKey: ['needs'], queryFn: () => needService.getAll(), }) const { addToCompare, removeFromCompare, clearCompare, isInCompare, compareTray } = useCompareStore() const activeNeed = needResp?.data?.[0] const filtered = filterSource === 'ALL' ? results : results.filter(r => r.resultType === filterSource) const sorted = sortResults(filtered, sortBy) const verifiedCount = results.filter(r => r.resultType === 'VERIFIED_PORTFOLIO').length const externalCount = results.filter(r => r.resultType === 'EXTERNAL_MARKET').length const futureCount = results.filter(r => r.resultType === 'FUTURE_AVAILABILITY').length const handleCompare = (propertyId: string) => { if (isInCompare(propertyId)) removeFromCompare(propertyId) else addToCompare(propertyId) } return ( {activeNeed && ( Aktive Suche: {activeNeed.companyName} {activeNeed.assetType} · {activeNeed.requiredArea.min}–{activeNeed.requiredArea.max} m² ·{' '} {activeNeed.preferredLocations.join(', ')} )} {isLoading ? ( ) : sorted.length === 0 ? ( ) : ( )} {compareTray.length > 0 && ( {compareTray.length} Objekte zum Vergleich ausgewählt )} ) }