'use client';

import React from 'react';
import { Container } from '@/ui/Container';
import { Stack } from '@/ui/Stack';
import { RacesLiveRail } from '@/components/races/RacesLiveRail';
import { RacesCommandBar } from '@/components/races/RacesCommandBar';
import { NextUpRacePanel } from '@/components/races/NextUpRacePanel';
import { RacesDayGroup } from '@/components/races/RacesDayGroup';
import { RacesEmptyState } from '@/components/races/RacesEmptyState';
import { RaceFilterModal } from '@/components/races/RaceFilterModal';
import { PageHeader } from '@/components/shared/PageHeader';
import type { RacesViewData } from '@/lib/view-data/RacesViewData';

export interface RacesIndexTemplateProps {
  viewData: RacesViewData & {
    racesByDate: Array<{
      dateKey: string;
      dateLabel: string;
      races: any[];
    }>;
    nextUpRace?: any;
  };
  // Filters
  statusFilter: string;
  setStatusFilter: (filter: any) => void;
  leagueFilter: string;
  setLeagueFilter: (filter: string) => void;
  timeFilter: string;
  setTimeFilter: (filter: any) => void;
  // Actions
  onRaceClick: (raceId: string) => void;
  // UI State
  showFilterModal: boolean;
  setShowFilterModal: (show: boolean) => void;
}

export function RacesIndexTemplate({
  viewData,
  statusFilter,
  setStatusFilter,
  leagueFilter,
  setLeagueFilter,
  timeFilter,
  setTimeFilter,
  onRaceClick,
  showFilterModal,
  setShowFilterModal,
}: RacesIndexTemplateProps) {
  const hasRaces = viewData.racesByDate.length > 0;

  return (
    <Container size="lg">
      <Stack gap={8} paddingY={12}>
        <PageHeader 
          title="Races" 
          subtitle="Live Sessions & Upcoming Events" 
        />

        {/* 1. Status Rail: Live sessions first */}
        <RacesLiveRail 
          liveRaces={viewData.liveRaces} 
          onRaceClick={onRaceClick} 
        />

        {/* 2. Command Bar: Fast filters */}
        <RacesCommandBar 
          timeFilter={timeFilter}
          setTimeFilter={setTimeFilter}
          leagueFilter={leagueFilter}
          setLeagueFilter={setLeagueFilter}
          leagues={viewData.leagues}
          onShowMoreFilters={() => setShowFilterModal(true)}
        />

        {/* 3. Next Up: High signal panel */}
        {timeFilter === 'upcoming' && viewData.nextUpRace && (
          <NextUpRacePanel 
            race={viewData.nextUpRace} 
            onRaceClick={onRaceClick} 
          />
        )}

        {/* 4. Browse by Day: Grouped schedule */}
        {hasRaces ? (
          <Stack gap={8}>
            {viewData.racesByDate.map((group) => (
              <RacesDayGroup 
                key={group.dateKey}
                dateLabel={group.dateLabel}
                races={group.races}
                onRaceClick={onRaceClick}
              />
            ))}
          </Stack>
        ) : (
          <RacesEmptyState />
        )}

        <RaceFilterModal
          isOpen={showFilterModal}
          onClose={() => setShowFilterModal(false)}
          statusFilter={statusFilter as any}
          setStatusFilter={setStatusFilter}
          leagueFilter={leagueFilter}
          setLeagueFilter={setLeagueFilter}
          timeFilter={timeFilter as any}
          setTimeFilter={setTimeFilter}
          searchQuery=""
          setSearchQuery={() => {}}
          leagues={viewData.leagues}
          showSearch={true}
          showTimeFilter={false}
        />
      </Stack>
    </Container>
  );
}
