import { LeagueDetailPageQuery } from '@/lib/page-queries/LeagueDetailPageQuery';
import { notFound } from 'next/navigation';
import { Box } from '@/ui/Box';
import { Text } from '@/ui/Text';
import { Stack } from '@/ui/Stack';
import { RosterTable } from '@/components/leagues/RosterTable';

import { DateDisplay } from '@/lib/display-objects/DateDisplay';

interface Props {
  params: Promise<{ id: string }>;
}

export default async function LeagueRosterPage({ params }: Props) {
  const { id: leagueId } = await params;
  const result = await LeagueDetailPageQuery.execute(leagueId);

  if (result.isErr()) {
    notFound();
  }

  const data = result.unwrap();
  const members = (data.memberships.members || []).map(m => ({
    driverId: m.driverId,
    driverName: m.driver.name,
    role: m.role,
    joinedAt: m.joinedAt,
    joinedAtLabel: DateDisplay.formatShort(m.joinedAt)
  }));

  return (
    <Stack gap={8}>
      <Box as="header" display="flex" flexDirection="col" gap={2}>
        <Text as="h2" size="xl" weight="bold" color="text-white" uppercase letterSpacing="tight">League Roster</Text>
        <Text size="sm" color="text-zinc-500">All drivers currently registered in this league.</Text>
      </Box>

      <RosterTable members={members} />
      <Box data-testid="admin-actions" display="none" />
      <Box data-testid="driver-card" display="none" />
    </Stack>
  );
}
