'use client'; import { useState, useMemo } from 'react'; import { useRouter } from 'next/navigation'; import { Users, Trophy, Search, Plus, Sparkles, Crown, Star, TrendingUp, Shield, Zap, UserPlus, ChevronRight, Timer, Target, Award, Handshake, MessageCircle, Calendar, } from 'lucide-react'; import TeamCard from '@/components/teams/TeamCard'; import Button from '@/components/ui/Button'; import Card from '@/components/ui/Card'; import Input from '@/components/ui/Input'; import Heading from '@/components/ui/Heading'; import CreateTeamForm from '@/components/teams/CreateTeamForm'; import WhyJoinTeamSection from '@/components/teams/WhyJoinTeamSection'; import SkillLevelSection from '@/components/teams/SkillLevelSection'; import FeaturedRecruiting from '@/components/teams/FeaturedRecruiting'; import TeamLeaderboardPreview from '@/components/teams/TeamLeaderboardPreview'; import { useAllTeams } from '@/hooks/useTeamService'; import type { TeamSummaryViewModel } from '@/lib/view-models/TeamSummaryViewModel'; // ============================================================================ // TYPES // ============================================================================ type TeamDisplayData = TeamSummaryViewModel; type SkillLevel = 'pro' | 'advanced' | 'intermediate' | 'beginner'; // ============================================================================ // SKILL LEVEL CONFIG // ============================================================================ const SKILL_LEVELS: { id: SkillLevel; label: string; icon: React.ElementType; color: string; bgColor: string; borderColor: string; description: string; }[] = [ { id: 'pro', label: 'Pro', icon: Crown, color: 'text-yellow-400', bgColor: 'bg-yellow-400/10', borderColor: 'border-yellow-400/30', description: 'Elite competition, sponsored teams', }, { id: 'advanced', label: 'Advanced', icon: Star, color: 'text-purple-400', bgColor: 'bg-purple-400/10', borderColor: 'border-purple-400/30', description: 'Competitive racing, high consistency', }, { id: 'intermediate', label: 'Intermediate', icon: TrendingUp, color: 'text-primary-blue', bgColor: 'bg-primary-blue/10', borderColor: 'border-primary-blue/30', description: 'Growing skills, regular practice', }, { id: 'beginner', label: 'Beginner', icon: Shield, color: 'text-green-400', bgColor: 'bg-green-400/10', borderColor: 'border-green-400/30', description: 'Learning the basics, friendly environment', }, ]; // ============================================================================ // MAIN PAGE COMPONENT // ============================================================================ export default function TeamsPage() { const router = useRouter(); const { data: teams = [], isLoading: loading } = useAllTeams(); const [searchQuery, setSearchQuery] = useState(''); const [showCreateForm, setShowCreateForm] = useState(false); // Derive groups by skill level from the loaded teams const groupsBySkillLevel = useMemo(() => { const byLevel: Record = { beginner: [], intermediate: [], advanced: [], pro: [], }; teams.forEach((team) => { const level = team.performanceLevel || 'intermediate'; if (byLevel[level]) { byLevel[level].push(team); } }); return byLevel; }, [teams]); // Select top teams by rating for the preview section const topTeams = useMemo(() => { const sortedByRating = [...teams].sort((a, b) => { // Rating is not currently part of TeamSummaryViewModel in this build. // Keep deterministic ordering by name until a rating field is exposed. return a.name.localeCompare(b.name); }); return sortedByRating.slice(0, 5); }, [teams]); const handleTeamClick = (teamId: string) => { if (teamId.startsWith('demo-team-')) { return; } router.push(`/teams/${teamId}`); }; const handleCreateSuccess = (teamId: string) => { setShowCreateForm(false); router.push(`/teams/${teamId}`); }; // Filter by search query const filteredTeams = teams.filter((team) => { if (!searchQuery) return true; const query = searchQuery.toLowerCase(); return ( team.name.toLowerCase().includes(query) || (team.description ?? '').toLowerCase().includes(query) || (team.region ?? '').toLowerCase().includes(query) || (team.languages ?? []).some((lang) => lang.toLowerCase().includes(query)) ); }); // Group teams by skill level const teamsByLevel = useMemo(() => { return SKILL_LEVELS.reduce( (acc, level) => { const fromGroup = groupsBySkillLevel[level.id] ?? []; acc[level.id] = filteredTeams.filter((team) => fromGroup.some((groupTeam) => groupTeam.id === team.id), ); return acc; }, { beginner: [], intermediate: [], advanced: [], pro: [], } as Record, ); }, [groupsBySkillLevel, filteredTeams]); const recruitingCount = teams.filter((t) => t.isRecruiting).length; if (showCreateForm) { return (

Create New Team

setShowCreateForm(false)} onSuccess={handleCreateSuccess} />
); } if (loading) { return (

Loading teams...

); } return (
{/* Hero Section - Different from Leagues */}
{/* Main Hero Card */}
{/* Background decorations */}
{/* Badge */}
Team Racing
Find Your Crew

Solo racing is great. Team racing is unforgettable. Join a team that matches your skill level and ambitions.

{/* Quick Stats */}
{teams.length} Teams
{recruitingCount} Recruiting
{/* CTA Buttons */}
{/* Skill Level Quick Nav */}

Find Your Level

{SKILL_LEVELS.map((level) => { const LevelIcon = level.icon; const count = teamsByLevel[level.id]?.length || 0; return ( ); })}
{/* Search and Filter Bar - Same style as Leagues */}
{/* Search */}
setSearchQuery(e.target.value)} className="pl-11" />
{/* Why Join Section */} {!searchQuery && } {/* Team Leaderboard Preview */} {!searchQuery && } {/* Featured Recruiting */} {!searchQuery && } {/* Teams by Skill Level */} {teams.length === 0 ? (
No teams yet

Be the first to create a racing team. Gather drivers and compete together in endurance events.

) : filteredTeams.length === 0 ? (

No teams found matching "{searchQuery}"

) : (
{SKILL_LEVELS.map((level, index) => (
))}
)}
); }