import { memo } from "react";
import {
  Text,
  Stack,
  Group,
  Box,
  Container,
  Divider,
  UnstyledButton,
} from "@mantine/core";
import { Player } from "../types";
import { usePlayersActivity } from "../queries";

interface PlayerActivityItemProps {
  player: Player;
}

const PlayerActivityItem = memo(({ player }: PlayerActivityItemProps) => {
  const playerName = player.first_name && player.last_name
    ? `${player.first_name} ${player.last_name}`
    : player.first_name || player.last_name || "Unknown Player";

  const formatDate = (dateStr?: string) => {
    if (!dateStr) return "Never";
    const date = new Date(dateStr);
    return date.toLocaleString();
  };

  const getTimeSince = (dateStr?: string) => {
    if (!dateStr) return "Never active";
    const date = new Date(dateStr);
    const now = new Date();
    const diffMs = now.getTime() - date.getTime();
    const diffMins = Math.floor(diffMs / 60000);
    const diffHours = Math.floor(diffMins / 60);
    const diffDays = Math.floor(diffHours / 24);

    if (diffMins < 1) return "Just now";
    if (diffMins < 60) return `${diffMins}m ago`;
    if (diffHours < 24) return `${diffHours}h ago`;
    if (diffDays < 30) return `${diffDays}d ago`;
    return formatDate(dateStr);
  };

  const isActive = player.last_activity &&
    (new Date().getTime() - new Date(player.last_activity).getTime()) < 5 * 60 * 1000;

  return (
    <Box
      w="100%"
      p="md"
      style={{
        borderRadius: 0,
      }}
    >
      <Group justify="space-between" align="flex-start" w="100%">
        <Stack gap={4} flex={1}>
          <Group gap="xs">
            <Text size="sm" fw={600}>
              {playerName}
            </Text>
            {isActive && (
              <Box
                w={8}
                h={8}
                style={{
                  borderRadius: "50%",
                  backgroundColor: "var(--mantine-color-green-6)",
                }}
              />
            )}
          </Group>
          <Group gap="md">
            <Text size="xs" c="dimmed">
              {getTimeSince(player.last_activity)}
            </Text>
            {player.last_activity && (
              <Text size="xs" c="dimmed">
                {formatDate(player.last_activity)}
              </Text>
            )}
          </Group>
        </Stack>
      </Group>
    </Box>
  );
});

export const PlayersActivityTable = () => {
  const { data: players } = usePlayersActivity();

  return (
    <Container size="100%" px={0}>
      <Stack gap="xs">
        <Group px="md" justify="space-between" align="center">
          <Text size="10px" lh={0} c="dimmed">
            {players.length} players
          </Text>
        </Group>

        <Stack gap={0}>
          {players.map((player: Player, index: number) => (
            <Box key={player.id}>
              <PlayerActivityItem player={player} />
              {index < players.length - 1 && <Divider />}
            </Box>
          ))}
        </Stack>

        {players.length === 0 && (
          <Text ta="center" c="dimmed" py="xl">
            No player activity found
          </Text>
        )}
      </Stack>
    </Container>
  );
};
