'use client';

import { useRouter } from 'next/navigation';
import DriverRankingsTemplate from '@/templates/DriverRankingsTemplate';
import { useState } from 'react';
import type { DriverLeaderboardViewModel } from '@/lib/view-models/DriverLeaderboardViewModel';

type SkillLevel = 'pro' | 'advanced' | 'intermediate' | 'beginner';
type SortBy = 'rank' | 'rating' | 'wins' | 'podiums' | 'winRate';

export function DriverRankingsPageWrapper({ data }: { data: DriverLeaderboardViewModel | null }) {
  const router = useRouter();
  
  // Client-side state for filtering and sorting
  const [searchQuery, setSearchQuery] = useState('');
  const [selectedSkill, setSelectedSkill] = useState<'all' | SkillLevel>('all');
  const [sortBy, setSortBy] = useState<SortBy>('rank');
  const [showFilters, setShowFilters] = useState(false);

  if (!data || !data.drivers) {
    return null;
  }

  const handleDriverClick = (driverId: string) => {
    if (driverId.startsWith('demo-')) return;
    router.push(`/drivers/${driverId}`);
  };

  const handleBackToLeaderboards = () => {
    router.push('/leaderboards');
  };

  return (
    <DriverRankingsTemplate
      drivers={data.drivers}
      searchQuery={searchQuery}
      selectedSkill={selectedSkill}
      sortBy={sortBy}
      showFilters={showFilters}
      onSearchChange={setSearchQuery}
      onSkillChange={setSelectedSkill}
      onSortChange={setSortBy}
      onToggleFilters={() => setShowFilters(!showFilters)}
      onDriverClick={handleDriverClick}
      onBackToLeaderboards={handleBackToLeaderboards}
    />
  );
}