'use client'; import { useState, useEffect } from 'react'; import { useRouter, useSearchParams } from 'next/navigation'; import { motion, useReducedMotion, AnimatePresence } from 'framer-motion'; import Link from 'next/link'; import Card from '@/components/ui/Card'; import Button from '@/components/ui/Button'; import StatusBadge from '@/components/ui/StatusBadge'; import InfoBanner from '@/components/ui/InfoBanner'; import { useServices } from '@/lib/services/ServiceProvider'; import { SponsorSponsorshipsViewModel } from '@/lib/view-models/SponsorSponsorshipsViewModel'; import { Megaphone, Trophy, Users, Eye, Calendar, ExternalLink, Plus, ChevronRight, Check, Clock, XCircle, Car, Flag, Search, Filter, TrendingUp, BarChart3, ArrowUpRight, ArrowDownRight, AlertCircle, Send, ThumbsUp, ThumbsDown, RefreshCw, Handshake } from 'lucide-react'; // ============================================================================ // Types // ============================================================================ type SponsorshipType = 'all' | 'leagues' | 'teams' | 'drivers' | 'races' | 'platform'; type SponsorshipStatus = 'all' | 'active' | 'pending_approval' | 'approved' | 'rejected' | 'expired'; interface Sponsorship { id: string; type: SponsorshipType; entityId: string; entityName: string; tier?: 'main' | 'secondary'; status: 'active' | 'pending_approval' | 'approved' | 'rejected' | 'expired'; applicationDate?: Date; approvalDate?: Date; rejectionReason?: string; startDate: Date; endDate: Date; price: number; impressions: number; impressionsChange?: number; engagement?: number; details?: string; // For pending approvals entityOwner?: string; applicationMessage?: string; } // ============================================================================ // Mock Data - Updated to show application workflow // ============================================================================ // ============================================================================ // Configuration // ============================================================================ const TYPE_CONFIG = { leagues: { icon: Trophy, color: 'text-primary-blue', bgColor: 'bg-primary-blue/10', label: 'League' }, teams: { icon: Users, color: 'text-purple-400', bgColor: 'bg-purple-400/10', label: 'Team' }, drivers: { icon: Car, color: 'text-performance-green', bgColor: 'bg-performance-green/10', label: 'Driver' }, races: { icon: Flag, color: 'text-warning-amber', bgColor: 'bg-warning-amber/10', label: 'Race' }, platform: { icon: Megaphone, color: 'text-racing-red', bgColor: 'bg-racing-red/10', label: 'Platform' }, all: { icon: BarChart3, color: 'text-gray-400', bgColor: 'bg-gray-400/10', label: 'All' }, }; const STATUS_CONFIG = { active: { icon: Check, color: 'text-performance-green', bgColor: 'bg-performance-green/10', borderColor: 'border-performance-green/30', label: 'Active' }, pending_approval: { icon: Clock, color: 'text-warning-amber', bgColor: 'bg-warning-amber/10', borderColor: 'border-warning-amber/30', label: 'Awaiting Approval' }, approved: { icon: ThumbsUp, color: 'text-primary-blue', bgColor: 'bg-primary-blue/10', borderColor: 'border-primary-blue/30', label: 'Approved' }, rejected: { icon: ThumbsDown, color: 'text-racing-red', bgColor: 'bg-racing-red/10', borderColor: 'border-racing-red/30', label: 'Declined' }, expired: { icon: XCircle, color: 'text-gray-400', bgColor: 'bg-gray-400/10', borderColor: 'border-gray-400/30', label: 'Expired' }, }; // ============================================================================ // Components // ============================================================================ function SponsorshipCard({ sponsorship }: { sponsorship: any }) { const router = useRouter(); const shouldReduceMotion = useReducedMotion(); const typeConfig = TYPE_CONFIG[sponsorship.type as keyof typeof TYPE_CONFIG]; const statusConfig = STATUS_CONFIG[sponsorship.status as keyof typeof STATUS_CONFIG]; const TypeIcon = typeConfig.icon; const StatusIcon = statusConfig.icon; const daysRemaining = Math.ceil((sponsorship.endDate.getTime() - Date.now()) / (1000 * 60 * 60 * 24)); const isExpiringSoon = daysRemaining > 0 && daysRemaining <= 30; const isPending = sponsorship.status === 'pending_approval'; const isRejected = sponsorship.status === 'rejected'; const isApproved = sponsorship.status === 'approved'; const getEntityLink = () => { switch (sponsorship.type) { case 'leagues': return `/leagues/${sponsorship.entityId}`; case 'teams': return `/teams/${sponsorship.entityId}`; case 'drivers': return `/drivers/${sponsorship.entityId}`; case 'races': return `/races/${sponsorship.entityId}`; default: return '#'; } }; return ( {/* Header */}
{typeConfig.label} {sponsorship.tier && ( {sponsorship.tier === 'main' ? 'Main Sponsor' : 'Secondary'} )}
{statusConfig.label}
{/* Entity Name */}

{sponsorship.entityName}

{sponsorship.details && (

{sponsorship.details}

)} {/* Application/Approval Info for non-active states */} {isPending && (
Application Pending

Sent to {sponsorship.entityOwner} on{' '} {sponsorship.applicationDate?.toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: 'numeric' })}

{sponsorship.applicationMessage && (

"{sponsorship.applicationMessage}"

)}
)} {isApproved && (
Approved!

Approved by {sponsorship.entityOwner} on{' '} {sponsorship.approvalDate?.toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: 'numeric' })}

Starts {sponsorship.startDate.toLocaleDateString('en-US', { month: 'short', day: 'numeric' })}

)} {isRejected && (
Application Declined
{sponsorship.rejectionReason && (

Reason: {sponsorship.rejectionReason}

)}
)} {/* Metrics Grid - Only show for active sponsorships */} {sponsorship.status === 'active' && (
Impressions
{sponsorship.formattedImpressions} {sponsorship.impressionsChange !== undefined && sponsorship.impressionsChange !== 0 && ( 0 ? 'text-performance-green' : 'text-racing-red' }`}> {sponsorship.impressionsChange > 0 ? : } {Math.abs(sponsorship.impressionsChange)}% )}
{sponsorship.engagement && (
Engagement
{sponsorship.engagement}%
)}
Period
{sponsorship.startDate.toLocaleDateString('en-US', { month: 'short', year: 'numeric' })} - {sponsorship.endDate.toLocaleDateString('en-US', { month: 'short', year: 'numeric' })}
Investment
{sponsorship.formattedPrice}
)} {/* Basic info for non-active */} {sponsorship.status !== 'active' && (
{sponsorship.periodDisplay}
{sponsorship.formattedPrice}
)} {/* Footer */}
{sponsorship.status === 'active' && ( {daysRemaining > 0 ? `${daysRemaining} days remaining` : 'Ended'} )} {isPending && ( Waiting for response... )}
{sponsorship.type !== 'platform' && ( )} {isPending && ( )} {sponsorship.status === 'active' && ( )}
); } // ============================================================================ // Main Component // ============================================================================ export default function SponsorCampaignsPage() { const router = useRouter(); const searchParams = useSearchParams(); const shouldReduceMotion = useReducedMotion(); const { sponsorService } = useServices(); const initialType = (searchParams.get('type') as SponsorshipType) || 'all'; const [typeFilter, setTypeFilter] = useState(initialType); const [statusFilter, setStatusFilter] = useState('all'); const [searchQuery, setSearchQuery] = useState(''); const [data, setData] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { const loadSponsorships = async () => { try { const sponsorshipsData = await sponsorService.getSponsorSponsorships('demo-sponsor-1'); if (sponsorshipsData) { setData(sponsorshipsData); } else { setError('Failed to load sponsorships data'); } } catch (err) { console.error('Error loading sponsorships:', err); setError('Failed to load sponsorships data'); } finally { setLoading(false); } }; loadSponsorships(); }, []); if (loading) { return (

Loading sponsorships...

); } if (error || !data) { return (

{error || 'No sponsorships data available'}

); } // Filter sponsorships const filteredSponsorships = data.sponsorships.filter(s => { if (typeFilter !== 'all' && s.type !== typeFilter) return false; if (statusFilter !== 'all' && s.status !== statusFilter) return false; if (searchQuery && !s.entityName.toLowerCase().includes(searchQuery.toLowerCase())) return false; return true; }); // Calculate stats const stats = { total: data.sponsorships.length, active: data.sponsorships.filter(s => s.status === 'active').length, pending: data.sponsorships.filter(s => s.status === 'pending_approval').length, approved: data.sponsorships.filter(s => s.status === 'approved').length, rejected: data.sponsorships.filter(s => s.status === 'rejected').length, totalInvestment: data.sponsorships.filter(s => s.status === 'active').reduce((sum, s) => sum + s.price, 0), totalImpressions: data.sponsorships.reduce((sum, s) => sum + s.impressions, 0), }; // Stats by type const statsByType = { leagues: data.sponsorships.filter(s => s.type === 'leagues').length, teams: data.sponsorships.filter(s => s.type === 'teams').length, drivers: data.sponsorships.filter(s => s.type === 'drivers').length, races: data.sponsorships.filter(s => s.type === 'races').length, platform: data.sponsorships.filter(s => s.type === 'platform').length, }; if (loading) { return (

Loading sponsorships...

); } return (
{/* Header */}

My Sponsorships

Manage applications and active sponsorship campaigns

{/* Info Banner about how sponsorships work */} {stats.pending > 0 && (

You have {stats.pending} pending application{stats.pending !== 1 ? 's' : ''} waiting for approval. League admins, team owners, and drivers review applications before accepting sponsorships.

)} {/* Quick Stats */}
{stats.total}
Total
{stats.active}
Active
0 ? 'border-warning-amber/30' : ''}`}>
{stats.pending}
Pending
{stats.approved}
Approved
${stats.totalInvestment.toLocaleString()}
Active Investment
{(stats.totalImpressions / 1000).toFixed(0)}k
Impressions
{/* Filters */}
{/* Search */}
setSearchQuery(e.target.value)} className="w-full pl-10 pr-4 py-2.5 rounded-lg border border-charcoal-outline bg-iron-gray text-white placeholder-gray-500 focus:border-primary-blue focus:outline-none" />
{/* Type Filter */}
{(['all', 'leagues', 'teams', 'drivers', 'races', 'platform'] as const).map((type) => { const config = TYPE_CONFIG[type]; const Icon = config.icon; const count = type === 'all' ? stats.total : statsByType[type]; return ( ); })}
{/* Status Filter */}
{(['all', 'active', 'pending_approval', 'approved', 'rejected'] as const).map((status) => { const config = status === 'all' ? { label: 'All', color: 'text-gray-400' } : STATUS_CONFIG[status]; const count = status === 'all' ? stats.total : data.sponsorships.filter(s => s.status === status).length; return ( ); })}
{/* Sponsorship List */} {filteredSponsorships.length === 0 ? (

No sponsorships found

{searchQuery || typeFilter !== 'all' || statusFilter !== 'all' ? 'Try adjusting your filters to see more results.' : 'Start sponsoring leagues, teams, or drivers to grow your brand visibility.'}

) : (
{filteredSponsorships.map((sponsorship) => ( ))}
)}
); }