'use client';
import { DriverIdentity } from '@/components/drivers/DriverIdentity';
import JoinLeagueButton from '@/components/leagues/JoinLeagueButton';
import LeagueActivityFeed from '@/components/leagues/LeagueActivityFeed';
import SponsorInsightsCard from '@/components/sponsors/SponsorInsightsCard';
import Button from '@/components/ui/Button';
import Card from '@/components/ui/Card';
import type { DriverSummaryData, LeagueDetailViewData, LeagueInfoData, LiveRaceData, SponsorInfo } from '@/lib/view-data/LeagueDetailViewData';
import { Calendar, ExternalLink, Star, Trophy, Users } from 'lucide-react';
import Image from 'next/image';
import { ReactNode } from 'react';
// ============================================================================
// TYPES
// ============================================================================
interface LeagueDetailTemplateProps {
viewData: LeagueDetailViewData;
leagueId: string;
isSponsor: boolean;
membership: { role: string } | null;
onMembershipChange: () => void;
onEndRaceModalOpen: (raceId: string) => void;
onLiveRaceClick: (raceId: string) => void;
children?: ReactNode;
}
interface LiveRaceCardProps {
races: LiveRaceData[];
membership: { role: string } | null;
onLiveRaceClick: (raceId: string) => void;
onEndRaceModalOpen: (raceId: string) => void;
}
interface LeagueInfoCardProps {
info: LeagueInfoData;
}
interface SponsorsSectionProps {
sponsors: SponsorInfo[];
}
interface ManagementSectionProps {
ownerSummary: DriverSummaryData | null;
adminSummaries: DriverSummaryData[];
stewardSummaries: DriverSummaryData[];
}
// ============================================================================
// LIVE RACE CARD COMPONENT
// ============================================================================
function LiveRaceCard({ races, membership, onLiveRaceClick, onEndRaceModalOpen }: LiveRaceCardProps) {
if (races.length === 0) return null;
return (
{races.map((race) => (
onLiveRaceClick(race.id)}
className="bg-performance-green hover:bg-performance-green/80 text-white"
>
View Live Race
{membership?.role === 'admin' && (
onEndRaceModalOpen(race.id)}
className="border-performance-green/50 text-performance-green hover:bg-performance-green/10"
>
End Race & Process Results
)}
Started {new Date(race.date).toLocaleDateString()}
{race.registeredCount && (
{race.registeredCount} drivers registered
)}
{race.strengthOfField && (
SOF: {race.strengthOfField}
)}
))}
);
}
// ============================================================================
// LEAGUE INFO CARD COMPONENT
// ============================================================================
function LeagueInfoCard({ info }: LeagueInfoCardProps) {
return (
About
{/* Stats Grid */}
{info.membersCount}
Members
{info.avgSOF ?? '—'}
Avg SOF
{/* Details */}
Structure
{info.structure}
Scoring
{info.scoring}
Created
{new Date(info.createdAt).toLocaleDateString('en-US', {
month: 'short',
year: 'numeric'
})}
{(info.discordUrl || info.youtubeUrl || info.websiteUrl) && (
)}
);
}
// ============================================================================
// SPONSORS SECTION COMPONENT
// ============================================================================
function SponsorsSection({ sponsors }: SponsorsSectionProps) {
if (sponsors.length === 0) return null;
return (
{sponsors.find(s => s.tier === 'main') ? 'Presented by' : 'Sponsors'}
{/* Main Sponsor - Featured prominently */}
{sponsors.filter(s => s.tier === 'main').map(sponsor => (
{sponsor.logoUrl ? (
) : (
)}
{sponsor.name}
Main
{sponsor.tagline && (
{sponsor.tagline}
)}
{sponsor.websiteUrl && (
)}
))}
{/* Secondary Sponsors - Smaller display */}
{sponsors.filter(s => s.tier === 'secondary').length > 0 && (
{sponsors.filter(s => s.tier === 'secondary').map(sponsor => (
{sponsor.logoUrl ? (
) : (
)}
{sponsor.name}
{sponsor.websiteUrl && (
)}
))}
)}
);
}
// ============================================================================
// MANAGEMENT SECTION COMPONENT
// ============================================================================
function ManagementSection({ ownerSummary, adminSummaries, stewardSummaries }: ManagementSectionProps) {
if (!ownerSummary && adminSummaries.length === 0 && stewardSummaries.length === 0) return null;
return (
Management
{ownerSummary && (
{ownerSummary.roleBadgeText}
)}
{adminSummaries.map((summary) => (
))}
{stewardSummaries.map((summary) => (
))}
);
}
// ============================================================================
// MAIN TEMPLATE COMPONENT
// ============================================================================
export function LeagueDetailTemplate({
viewData,
leagueId,
isSponsor,
membership,
onMembershipChange,
onEndRaceModalOpen,
onLiveRaceClick,
children,
}: LeagueDetailTemplateProps) {
return (
<>
{/* Sponsor Insights Card - Only shown to sponsors, at top of page */}
{isSponsor && viewData.sponsorInsights && (
)}
{/* Live Race Card - Prominently show running races */}
{viewData.runningRaces.length > 0 && (
)}
{/* Action Card */}
{!membership && !isSponsor && (
Join This League
Become a member to participate in races and track your progress
)}
{/* League Overview - Activity Center with Info Sidebar */}
{/* Center - Activity Feed */}
Recent Activity
{/* Right Sidebar - League Info */}
{/* League Info - Combined */}
{/* Sponsors Section - Show sponsor logos */}
{viewData.sponsors.length > 0 && (
)}
{/* Management */}
{/* Children (for modals, etc.) */}
{children}
>
);
}