'use client';

import React from 'react';
import { Trophy, ChevronLeft } from 'lucide-react';
import { Container } from '@/ui/Container';
import { PageHeader } from '@/ui/PageHeader';
import { RankingsPodium } from '@/components/leaderboards/RankingsPodium';
import { LeaderboardTable } from '@/components/leaderboards/LeaderboardTable';
import { Button } from '@/ui/Button';
import { Icon } from '@/ui/Icon';
import { LeaderboardFiltersBar } from '@/components/leaderboards/LeaderboardFiltersBar';
import type { DriverRankingsViewData } from '@/lib/view-data/DriverRankingsViewData';

interface DriverRankingsTemplateProps {
  viewData: DriverRankingsViewData;
  searchQuery: string;
  onSearchChange: (query: string) => void;
  onDriverClick?: (id: string) => void;
  onBackToLeaderboards?: () => void;
}

export function DriverRankingsTemplate({
  viewData,
  searchQuery,
  onSearchChange,
  onDriverClick,
  onBackToLeaderboards,
}: DriverRankingsTemplateProps): React.ReactElement {
  return (
    <Container size="lg" spacing="md">
      <PageHeader
        title="Driver Leaderboard"
        description="Full rankings of all drivers by performance metrics"
        icon={Trophy}
        action={
          onBackToLeaderboards && (
            <Button 
              variant="secondary" 
              onClick={onBackToLeaderboards} 
              icon={<Icon icon={ChevronLeft} size={4} />}
            >
              Back to Leaderboards
            </Button>
          )
        }
      />

      {/* Top 3 Podium */}
      {viewData.podium.length > 0 && !searchQuery && (
        <RankingsPodium 
          podium={viewData.podium.map(d => ({
            ...d,
            rating: Number(d.rating),
            wins: Number(d.wins),
            podiums: Number(d.podiums)
          }))} 
          onDriverClick={onDriverClick} 
        />
      )}

      <LeaderboardFiltersBar 
        searchQuery={searchQuery}
        onSearchChange={onSearchChange}
        placeholder="Search drivers..."
      />

      {/* Leaderboard Table */}
      <LeaderboardTable 
        drivers={viewData.drivers.map(d => ({
          ...d,
          rating: Number(d.rating),
          wins: Number(d.wins),
          racesCompleted: d.racesCompleted || 0,
          avatarUrl: d.avatarUrl || ''
        }))} 
        onDriverClick={onDriverClick} 
      />
    </Container>
  );
}
