import {
  Box,
  Text,
  Group,
  Stack,
  ThemeIcon,
  Skeleton,
  Divider,
} from "@mantine/core";
import {
  CrownIcon,
  XIcon,
  FireIcon,
  ShieldIcon,
  ChartLineUpIcon,
  ShieldCheckIcon,
  BoxingGloveIcon,
  Icon,
  ArrowUpIcon,
  ArrowDownIcon,
} from "@phosphor-icons/react";
import { Trans, useLingui } from "@lingui/react/macro";
import { BaseStats } from "@/types/stats";

interface StatsOverviewProps {
  statsData: BaseStats | null;
  isLoading?: boolean;
}

const StatItem = ({
  label,
  value,
  suffix = "",
  Icon,
}: {
  label: string;
  value: number | null;
  suffix?: string;
  Icon?: Icon;
  isLoading?: boolean;
}) => {
  return (
    <Group justify="space-between" align="center" py="md" px="sm">
      <Group gap="sm" align="center">
        {Icon && (
          <ThemeIcon size="md" variant="light" radius="sm" color="gray">
            <Icon size={16} />
          </ThemeIcon>
        )}
        <Text size="sm" fw={500}>
          {label}
        </Text>
      </Group>
      {value !== null ? (
        <Text size="sm" fw={700} c="dimmed">
          {`${value}${suffix}`}
        </Text>
      ) : (
        <Skeleton width={20} height={20} />
      )}
    </Group>
  );
};

const StatsOverview = ({ statsData, isLoading = false }: StatsOverviewProps) => {
  const { t } = useLingui();

  if (!statsData && !isLoading) {
    return (
      <Box p="sm" h="auto" mih={200}>
        <Text ta="center" size="sm" fw={600} c="dimmed">
          <Trans>No stats available yet</Trans>
        </Text>
      </Box>
    );
  }

  if (!statsData) return null;

  const overallStats = {
    matches: statsData.matches,
    wins: statsData.wins,
    losses: statsData.losses,
    total_cups_made: statsData.total_cups_made,
    total_cups_against: statsData.total_cups_against,
  };

  const avgCupsPerMatch =
    overallStats.matches > 0
      ? parseFloat((overallStats.total_cups_made / overallStats.matches).toFixed(1))
      : 0;

  const avgCupsAgainstPerMatch =
    overallStats.matches > 0
      ? parseFloat((overallStats.total_cups_against / overallStats.matches).toFixed(1))
      : 0;

  const avgMarginOfVictory = statsData.margin_of_victory ? parseFloat(statsData.margin_of_victory.toFixed(1)) : 0;
  const avgMarginOfLoss = statsData.margin_of_loss ? parseFloat(statsData.margin_of_loss.toFixed(1)) : 0;

  const allStats = [
    { label: t`Matches Played`, value: overallStats.matches, Icon: BoxingGloveIcon },
    { label: t`Wins`, value: overallStats.wins, Icon: CrownIcon },
    { label: t`Losses`, value: overallStats.losses, Icon: XIcon },
    { label: t`Cups Made`, value: overallStats.total_cups_made, Icon: FireIcon },
    { label: t`Cups Against`, value: overallStats.total_cups_against, Icon: ShieldIcon },
    { label: t`Avg Cups Per Match`, value: avgCupsPerMatch >= 0 ? avgCupsPerMatch : null, Icon: ChartLineUpIcon },
    { label: t`Avg Cups Against`, value: avgCupsAgainstPerMatch >= 0 ? avgCupsAgainstPerMatch : null, Icon: ShieldCheckIcon },
    { label: t`Avg Win Margin`, value: avgMarginOfVictory >= 0 ? avgMarginOfVictory : null, Icon: ArrowUpIcon },
    { label: t`Avg Loss Margin`, value: avgMarginOfLoss >= 0 ? avgMarginOfLoss : null, Icon: ArrowDownIcon },
  ];

  return (
    <Box>
      <Stack gap={0}>
        {allStats.map((stat, index) => (
          <Box key={stat.label}>
            <StatItem
              label={stat.label}
              value={stat.value}
              Icon={stat.Icon}
              isLoading={isLoading}
            />
            {index < allStats.length - 1 && <Divider />}
          </Box>
        ))}
      </Stack>
    </Box>
  );
};

export const StatsSkeleton = () => {
  const { t } = useLingui();
  const skeletonStats = [
    { label: t`Matches Played`, Icon: BoxingGloveIcon },
    { label: t`Wins`, Icon: CrownIcon },
    { label: t`Losses`, Icon: XIcon },
    { label: t`Cups Made`, Icon: FireIcon },
    { label: t`Cups Against`, Icon: ShieldIcon },
    { label: t`Avg Cups Per Match`, Icon: ChartLineUpIcon },
    { label: t`Avg Cups Against`, Icon: ShieldCheckIcon },
    { label: t`Avg Win Margin`, Icon: ArrowUpIcon },
    { label: t`Avg Loss Margin`, Icon: ArrowDownIcon },
  ];

  return (
    <Box>
      <Stack gap={0}>
        {skeletonStats.map((stat, index) => (
          <Box key={stat.label}>
            <StatItem
              label={stat.label}
              value={null}
              Icon={stat.Icon}
              isLoading={true}
            />
            {index < skeletonStats.length - 1 && <Divider />}
          </Box>
        ))}
      </Stack>
    </Box>
  );
};

export default StatsOverview;
