'use client'; import Breadcrumbs from '@/components/layout/Breadcrumbs'; import PendingSponsorshipRequests from '@/components/sponsors/PendingSponsorshipRequests'; import Button from '@/components/ui/Button'; import Card from '@/components/ui/Card'; import { useRouter } from 'next/navigation'; import { useCallback, useEffect, useState } from 'react'; import { useEffectiveDriverId } from '@/hooks/useEffectiveDriverId'; import { LeagueRoleUtility } from '@/lib/utilities/LeagueRoleUtility'; import { useServices } from '@/lib/services/ServiceProvider'; import { SponsorshipRequestViewModel } from '@/lib/view-models/SponsorshipRequestViewModel'; import { AlertTriangle, Building, ChevronRight, Handshake, Trophy, User, Users } from 'lucide-react'; import Link from 'next/link'; interface EntitySection { entityType: 'driver' | 'team' | 'race' | 'season'; entityId: string; entityName: string; requests: SponsorshipRequestViewModel[]; } 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 { sponsorshipService, driverService, leagueService, teamService, leagueMembershipService } = useServices(); const allSections: EntitySection[] = []; // 1. Driver's own sponsorship requests const driverRequests = await sponsorshipService.getPendingSponsorshipRequests({ entityType: 'driver', entityId: currentDriverId, }); if (driverRequests.length > 0) { const driverProfile = await driverService.getDriverProfile(currentDriverId); allSections.push({ entityType: 'driver', entityId: currentDriverId, entityName: driverProfile?.currentDriver?.name ?? 'Your Profile', requests: driverRequests, }); } // 2. Leagues where the user is admin/owner const allLeagues = await leagueService.getAllLeagues(); for (const league of allLeagues) { const membership = await leagueMembershipService.getMembership(league.id, currentDriverId); if (membership && LeagueRoleUtility.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 leagueRequests = await sponsorshipService.getPendingSponsorshipRequests({ entityType: 'season', entityId: league.id, // Using league ID as a proxy for now }); if (leagueRequests.length > 0) { allSections.push({ entityType: 'season', entityId: league.id, entityName: league.name, requests: leagueRequests, }); } } catch (err) { // Silently skip if no requests found } } } // 3. Teams where the user is owner/manager const allTeams = await teamService.getAllTeams(); for (const team of allTeams) { const membership = await teamService.getMembership(team.id, currentDriverId); if (membership && (membership.role === 'owner' || membership.role === 'manager')) { const teamRequests = await sponsorshipService.getPendingSponsorshipRequests({ entityType: 'team', entityId: team.id, }); if (teamRequests.length > 0) { allSections.push({ entityType: 'team', entityId: team.id, entityName: team.name, requests: teamRequests, }); } } } 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 { sponsorshipService } = useServices(); await sponsorshipService.acceptSponsorshipRequest(requestId, currentDriverId); await loadAllRequests(); }; const handleReject = async (requestId: string, reason?: string) => { const { sponsorshipService } = useServices(); await sponsorshipService.rejectSponsorshipRequest(requestId, currentDriverId, reason); 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.

); }