import {
  Card,
  Text,
  Stack,
  Group,
  UnstyledButton,
  Badge,
  Title,
} from "@mantine/core";
import { TournamentInfo } from "@/features/tournaments/types";
import {
  TrophyIcon,
  CrownIcon,
  MedalIcon,
} from "@phosphor-icons/react";
import { useNavigate } from "@tanstack/react-router";
import Avatar from "@/components/avatar";

interface TournamentCardProps {
  tournament: TournamentInfo;
}

export const TournamentCard = ({ tournament }: TournamentCardProps) => {
  const navigate = useNavigate();

  return (
    <UnstyledButton
      w="100%"
      onClick={() => navigate({ to: `/tournaments/${tournament.id}` })}
      style={{ borderRadius: "var(--mantine-radius-md)" }}
    >
      <Card
        withBorder
        radius="md"
        p="lg"
        w="100%"
        style={{
          transition: "all 0.15s ease",
          border: "1px solid var(--mantine-color-default-border)",
        }}
      >
        <Group justify="space-between" align="center">
          <Group gap="md" align="center">
            <Avatar
              size={75}
              radius="sm"
              name={tournament.name}
              contain
              src={
                tournament.logo
                  ? `/api/files/tournaments/${tournament.id}/${tournament.logo}`
                  : undefined
              }
            >
              <TrophyIcon size={20} />
            </Avatar>
            <Stack gap="xs">
              <Title mb={-6} order={3} lineClamp={2}>
                {tournament.name}
              </Title>
              {(tournament.first_place || tournament.second_place || tournament.third_place) && (
                <Stack gap={6} >
                  {tournament.first_place && (
                    <Badge
                      size="sm"
                      radius="md"
                      variant="filled"
                      color="yellow"
                      leftSection={
                        <CrownIcon size={16} />
                      }
                      style={{
                        textTransform: 'none',
                        fontWeight: 600,
                        color: 'white',
                      }}
                    >
                      {tournament.first_place.name}
                    </Badge>
                  )}
                  {tournament.second_place && (
                    <Badge
                      size="sm"
                      radius="md"
                      color="gray"
                      variant="filled"
                      leftSection={
                        <MedalIcon size={16} />
                      }
                      style={{
                        textTransform: 'none',
                        fontWeight: 500,
                      }}
                    >
                      {tournament.second_place.name}
                    </Badge>
                  )}
                  {tournament.third_place && (
                    <Badge
                      size="sm"
                      radius="md"
                      color="orange"
                      variant="filled"
                      leftSection={
                        <MedalIcon size={16} />
                      }
                      style={{
                        textTransform: 'none',
                        fontWeight: 500,
                      }}
                    >
                      {tournament.third_place.name}
                    </Badge>
                  )}
                </Stack>
              )}
            </Stack>
          </Group>
        </Group>
      </Card>
    </UnstyledButton>
  );
};
