'use client'; import { useState, useEffect, useCallback } from 'react'; import { useRouter } from 'next/navigation'; import Card from '@/components/ui/Card'; import Button from '@/components/ui/Button'; import Breadcrumbs from '@/components/layout/Breadcrumbs'; import PendingSponsorshipRequests, { type PendingRequestDTO } from '@/components/sponsors/PendingSponsorshipRequests'; import { getGetPendingSponsorshipRequestsUseCase, getAcceptSponsorshipRequestUseCase, getRejectSponsorshipRequestUseCase, getDriverRepository, getLeagueRepository, getTeamRepository, getLeagueMembershipRepository, getTeamMembershipRepository, } from '@/lib/di-container'; import { PendingSponsorshipRequestsPresenter } from '@/lib/presenters/PendingSponsorshipRequestsPresenter'; import { useEffectiveDriverId } from '@/lib/currentDriver'; import { isLeagueAdminOrHigherRole } from '@/lib/leagueRoles'; import { Handshake, User, Users, Trophy, ChevronRight, Building, AlertTriangle } from 'lucide-react'; import Link from 'next/link'; interface EntitySection { entityType: 'driver' | 'team' | 'race' | 'season'; entityId: string; entityName: string; requests: PendingRequestDTO[]; } export default function SponsorshipRequestsPage() { const router = useRouter(); const currentDriverId = useEffectiveDriverId(); const [sections, setSections] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const loadAllRequests = useCallback(async () => { setLoading(true); setError(null); try { const driverRepo = getDriverRepository(); const leagueRepo = getLeagueRepository(); const teamRepo = getTeamRepository(); const leagueMembershipRepo = getLeagueMembershipRepository(); const teamMembershipRepo = getTeamMembershipRepository(); const useCase = getGetPendingSponsorshipRequestsUseCase(); const allSections: EntitySection[] = []; // 1. Driver's own sponsorship requests const driverPresenter = new PendingSponsorshipRequestsPresenter(); await useCase.execute( { entityType: 'driver', entityId: currentDriverId, }, driverPresenter, ); const driverResult = driverPresenter.getViewModel(); if (driverResult && driverResult.requests.length > 0) { const driver = await driverRepo.findById(currentDriverId); allSections.push({ entityType: 'driver', entityId: currentDriverId, entityName: driver?.name ?? 'Your Profile', requests: driverResult.requests, }); } // 2. Leagues where the user is admin/owner const allLeagues = await leagueRepo.findAll(); for (const league of allLeagues) { const membership = await leagueMembershipRepo.getMembership(league.id, currentDriverId); if (membership && isLeagueAdminOrHigherRole(membership.role)) { // Load sponsorship requests for this league's active season try { // For simplicity, we'll query by season entityType - in production you'd get the active season ID const leaguePresenter = new PendingSponsorshipRequestsPresenter(); await useCase.execute( { entityType: 'season', entityId: league.id, // Using league ID as a proxy for now }, leaguePresenter, ); const leagueResult = leaguePresenter.getViewModel(); if (leagueResult && leagueResult.requests.length > 0) { allSections.push({ entityType: 'season', entityId: league.id, entityName: league.name, requests: leagueResult.requests, }); } } catch (err) { // Silently skip if no requests found } } } // 3. Teams where the user is owner/manager const allTeams = await teamRepo.findAll(); for (const team of allTeams) { const membership = await teamMembershipRepo.getMembership(team.id, currentDriverId); if (membership && (membership.role === 'owner' || membership.role === 'manager')) { const teamPresenter = new PendingSponsorshipRequestsPresenter(); await useCase.execute( { entityType: 'team', entityId: team.id, }, teamPresenter, ); const teamResult = teamPresenter.getViewModel(); if (teamResult && teamResult.requests.length > 0) { allSections.push({ entityType: 'team', entityId: team.id, entityName: team.name, requests: teamResult.requests, }); } } } setSections(allSections); } catch (err) { console.error('Failed to load sponsorship requests:', err); setError(err instanceof Error ? err.message : 'Failed to load requests'); } finally { setLoading(false); } }, [currentDriverId]); useEffect(() => { loadAllRequests(); }, [loadAllRequests]); const handleAccept = async (requestId: string) => { const useCase = getAcceptSponsorshipRequestUseCase(); await useCase.execute({ requestId, respondedBy: currentDriverId, }); await loadAllRequests(); }; const handleReject = async (requestId: string, reason?: string) => { const useCase = getRejectSponsorshipRequestUseCase(); const input: { requestId: string; respondedBy: string; reason?: string } = { requestId, respondedBy: currentDriverId, }; if (typeof reason === 'string') { input.reason = reason; } await useCase.execute(input); await loadAllRequests(); }; const getEntityIcon = (type: 'driver' | 'team' | 'race' | 'season') => { switch (type) { case 'driver': return User; case 'team': return Users; case 'race': return Trophy; case 'season': return Trophy; default: return Building; } }; const getEntityLink = (type: 'driver' | 'team' | 'race' | 'season', id: string) => { switch (type) { case 'driver': return `/drivers/${id}`; case 'team': return `/teams/${id}`; case 'race': return `/races/${id}`; case 'season': return `/leagues/${id}/sponsorships`; default: return '#'; } }; const totalRequests = sections.reduce((sum, s) => sum + s.requests.length, 0); return (
{/* Header */}

Sponsorship Requests

Manage sponsorship requests for your profile, teams, and leagues

{totalRequests > 0 && (
{totalRequests} pending
)}
{loading ? (
Loading sponsorship requests...
) : error ? (

Error Loading Requests

{error}

) : sections.length === 0 ? (

No Pending Requests

You don't have any pending sponsorship requests at the moment.

Sponsors can apply to sponsor your profile, teams, or leagues you manage.

) : (
{sections.map((section) => { const Icon = getEntityIcon(section.entityType); const entityLink = getEntityLink(section.entityType, section.entityId); return ( {/* Section Header */}

{section.entityName}

{section.entityType}

View {section.entityType === 'season' ? 'Sponsorships' : section.entityType}
{/* Requests */}
); })}
)} {/* Info Card */}

How Sponsorships Work

Sponsors can apply to sponsor your driver profile, teams you manage, or leagues you administer. Review each request carefully - accepting will activate the sponsorship and the sponsor will be charged. You'll receive the payment minus a 10% platform fee.

); }