"use client";

import {
  Badge,
  Box,
  Card,
  Grid,
  HStack,
  Icon,
  IconButton,
  SimpleGrid,
  Text,
  VStack,
} from "@chakra-ui/react";
import {
  LuBadgeCheck,
  LuCircleHelp,
  LuRectangleVertical,
  LuShieldAlert,
  LuTarget,
  LuTrendingUp,
} from "react-icons/lu";
import { useColorModeValue } from "@/components/ui/color-mode";
import { useTranslations } from "next-intl";
import { Tooltip } from "@/components/ui/overlays/tooltip";
import type {
  HtftComboKey,
  OddsBandCardsDto,
  OddsBandHtftComboDto,
  TripleValueEntryDto,
  V27EngineDto,
} from "@/lib/api/predictions/types";

// ──────────────────────────────────────
// Helpers
// ──────────────────────────────────────

function pct(v: number, d = 0): string {
  if (!v && v !== 0) return "-";
  return `${(v * 100).toFixed(d)}%`;
}

function edgeStr(edge: number): string {
  const sign = edge > 0 ? "+" : "";
  return `${sign}${(edge * 100).toFixed(1)}%`;
}

const TRIPLE_VALUE_KEYS = new Set([
  "home",
  "away",
  "ou25_over",
  "btts_yes",
  "ou15_over",
  "ou35_over",
  "dc_1x",
  "dc_x2",
  "dc_12",
  "ht_home",
  "ht_away",
  "ht_ou05_over",
  "ht_ou15_over",
  "oe_odd",
  "cards_over",
  "htft_11",
  "htft_1x",
  "htft_12",
  "htft_x1",
  "htft_xx",
  "htft_x2",
  "htft_21",
  "htft_2x",
  "htft_22",
]);

const HTFT_DISPLAY: Record<HtftComboKey, string> = {
  "11": "1/1",
  "1x": "1/X",
  "12": "1/2",
  x1: "X/1",
  xx: "X/X",
  x2: "X/2",
  "21": "2/1",
  "2x": "2/X",
  "22": "2/2",
};

const HTFT_ROWS: HtftComboKey[][] = [
  ["11", "1x", "12"],
  ["x1", "xx", "x2"],
  ["21", "2x", "22"],
];

function TooltipIcon({ content }: { content: string }) {
  return (
    <Tooltip
      content={content}
      showArrow
      positioning={{ placement: "top" }}
      contentProps={{ maxW: "260px", fontSize: "xs", px: 3, py: 2 }}
    >
      <IconButton
        aria-label="info"
        variant="ghost"
        size="2xs"
        colorPalette="gray"
      >
        <LuCircleHelp />
      </IconButton>
    </Tooltip>
  );
}

function SectionTitle({
  icon,
  title,
  info,
}: {
  icon: React.ElementType;
  title: string;
  info?: string;
}) {
  return (
    <HStack justify="space-between" w="full">
      <HStack gap={2}>
        <Icon as={icon} boxSize={4.5} color="fg.muted" />
        <Text fontSize="lg" fontWeight="semibold">
          {title}
        </Text>
      </HStack>
      {info ? <TooltipIcon content={info} /> : null}
    </HStack>
  );
}

// ──────────────────────────────────────
// Triple Value Card
// ──────────────────────────────────────

function TripleValueCard({
  label,
  entry,
}: {
  label: string;
  entry: TripleValueEntryDto;
}) {
  const t = useTranslations("v28");
  const isValue = entry.is_value;
  const hasSample = entry.band_sample >= 5;

  const cardBg = useColorModeValue(
    isValue ? "green.50" : "gray.50",
    isValue ? "green.950" : "whiteAlpha.50",
  );
  const borderCol = useColorModeValue(
    isValue ? "green.300" : "gray.200",
    isValue ? "green.700" : "gray.700",
  );
  const edgeColor =
    entry.edge > 0.03
      ? "green.500"
      : entry.edge < -0.03
        ? "red.400"
        : "fg.muted";

  return (
    <Box
      p={3}
      bg={cardBg}
      borderWidth="1px"
      borderColor={borderCol}
      borderRadius="xl"
      position="relative"
      overflow="hidden"
    >
      {isValue && (
        <Box
          position="absolute"
          top={0}
          left={0}
          right={0}
          h="3px"
          bgGradient="to-r"
          gradientFrom="green.400"
          gradientTo="teal.400"
        />
      )}
      <VStack align="stretch" gap={1.5}>
        <HStack justify="space-between">
          <Text fontSize="xs" fontWeight="semibold" color="fg.muted">
            {label}
          </Text>
          {isValue ? (
            <Badge
              colorPalette="green"
              variant="solid"
              fontSize="2xs"
              borderRadius="full"
            >
              {t("value")}
            </Badge>
          ) : hasSample ? (
            <Badge variant="outline" fontSize="2xs" borderRadius="full">
              {t("pass")}
            </Badge>
          ) : (
            <Badge
              colorPalette="gray"
              variant="subtle"
              fontSize="2xs"
              borderRadius="full"
            >
              {t("insufficient")}
            </Badge>
          )}
        </HStack>

        <Text fontSize="xl" fontWeight="bold" color={edgeColor}>
          {edgeStr(entry.edge)}
        </Text>

        <HStack gap={2} flexWrap="wrap">
          <Text fontSize="2xs" color="fg.muted">
            {t("band")}: {pct(entry.band_rate, 1)}
          </Text>
          <Text fontSize="2xs" color="fg.muted">
            {t("odds")}: {pct(entry.implied_prob, 1)}
          </Text>
          {entry.confirmations !== undefined && (
            <Text fontSize="2xs" color="fg.muted">
              {t("confirmations")}: {entry.confirmations}/2
            </Text>
          )}
        </HStack>

        <Text fontSize="2xs" color="fg.muted">
          {t("matches-count", { count: entry.band_sample })}
        </Text>
      </VStack>
    </Box>
  );
}

// ──────────────────────────────────────
// Cards Section
// ──────────────────────────────────────

function ProgressBar({
  value,
  max,
  color,
}: {
  value: number;
  max: number;
  color: string;
}) {
  const trackBg = useColorModeValue("gray.100", "gray.700");
  const w = max > 0 ? Math.min(100, (value / max) * 100) : 0;
  return (
    <Box h="10px" w="full" bg={trackBg} borderRadius="full" overflow="hidden">
      <Box
        h="full"
        w={`${w}%`}
        bg={color}
        borderRadius="full"
        transition="width 0.4s ease"
      />
    </Box>
  );
}

function CardsSection({ cards }: { cards: OddsBandCardsDto }) {
  const t = useTranslations("v28");
  const cardBg = useColorModeValue("white", "gray.800");
  const borderColor = useColorModeValue("gray.200", "gray.700");
  const hasData = cards.sample >= 3;

  if (!hasData) {
    return (
      <Box
        p={4}
        bg={cardBg}
        borderWidth="1px"
        borderColor={borderColor}
        borderRadius="xl"
      >
        <HStack gap={2} mb={2}>
          <Icon as={LuRectangleVertical} boxSize={4} color="yellow.500" />
          <Text fontSize="sm" fontWeight="semibold">
            {t("cards-analysis")}
          </Text>
        </HStack>
        <Text fontSize="sm" color="fg.muted">
          {t("insufficient-data")}
        </Text>
      </Box>
    );
  }

  const overPct = cards.combined_over_rate * 100;
  const overColor =
    overPct >= 65 ? "red.400" : overPct >= 50 ? "orange.400" : "green.400";

  return (
    <Box
      p={4}
      bg={cardBg}
      borderWidth="1px"
      borderColor={borderColor}
      borderRadius="xl"
    >
      <HStack justify="space-between" mb={4}>
        <HStack gap={2}>
          <Icon as={LuRectangleVertical} boxSize={4} color="yellow.500" />
          <Text fontSize="sm" fontWeight="semibold">
            {t("cards-analysis")}
          </Text>
        </HStack>
        <Badge variant="outline" fontSize="2xs" borderRadius="full">
          {t("matches-count", { count: cards.sample })}
        </Badge>
      </HStack>

      <VStack align="stretch" gap={3}>
        {/* Referee profile */}
        <Box>
          <HStack justify="space-between" mb={1}>
            <Text fontSize="xs" color="fg.muted">
              {t("referee-profile")}
            </Text>
            <Text fontSize="xs" fontWeight="semibold">
              {t("avg-cards", { avg: cards.referee_avg.toFixed(1) })}
            </Text>
          </HStack>
          <ProgressBar
            value={cards.referee_over_rate * 100}
            max={100}
            color="purple.400"
          />
          <HStack justify="space-between" mt={0.5}>
            <Text fontSize="2xs" color="fg.muted">
              {t("over-rate")}: {pct(cards.referee_over_rate, 0)}
            </Text>
            <Text fontSize="2xs" color="fg.muted">
              {t("matches-count", { count: cards.referee_sample })}
            </Text>
          </HStack>
        </Box>

        {/* Team profile */}
        <Box>
          <HStack justify="space-between" mb={1}>
            <Text fontSize="xs" color="fg.muted">
              {t("team-profile")}
            </Text>
            <Text fontSize="xs" fontWeight="semibold">
              {t("avg-cards", { avg: cards.team_avg.toFixed(1) })}
            </Text>
          </HStack>
          <ProgressBar
            value={cards.team_over_rate * 100}
            max={100}
            color="blue.400"
          />
          <HStack justify="space-between" mt={0.5}>
            <Text fontSize="2xs" color="fg.muted">
              {t("over-rate")}: {pct(cards.team_over_rate, 0)}
            </Text>
            <Text fontSize="2xs" color="fg.muted">
              {t("matches-count", { count: cards.team_sample })}
            </Text>
          </HStack>
        </Box>

        {/* Combined */}
        <Box
          p={3}
          bg={useColorModeValue("gray.50", "whiteAlpha.50")}
          borderRadius="lg"
        >
          <HStack justify="space-between">
            <VStack align="start" gap={0}>
              <Text fontSize="xs" fontWeight="semibold">
                {t("combined-over-rate")}
              </Text>
              <Text fontSize="2xs" color="fg.muted">
                {t("combined-weighting")}
              </Text>
            </VStack>
            <Text fontSize="2xl" fontWeight="bold" color={overColor}>
              {overPct.toFixed(0)}%
            </Text>
          </HStack>
        </Box>
      </VStack>
    </Box>
  );
}

// ──────────────────────────────────────
// HTFT 3x3 Grid
// ──────────────────────────────────────

function HtftGrid({
  htft,
}: {
  htft: Record<HtftComboKey, OddsBandHtftComboDto>;
}) {
  const t = useTranslations("v28");
  const cardBg = useColorModeValue("white", "gray.800");
  const borderColor = useColorModeValue("gray.200", "gray.700");

  // Find the max rate for highlighting
  let maxRate = 0;
  let maxKey: HtftComboKey = "11";
  let totalSample = 0;
  for (const [key, val] of Object.entries(htft) as [
    HtftComboKey,
    OddsBandHtftComboDto,
  ][]) {
    if (val.rate > maxRate) {
      maxRate = val.rate;
      maxKey = key;
    }
    totalSample = Math.max(totalSample, val.sample);
  }

  const getCellColor = (rate: number, isMax: boolean) => {
    if (isMax) return { bg: "green.500", text: "white" };
    if (rate >= 0.2) return { bg: "green.100", text: "green.800" };
    if (rate >= 0.12) return { bg: "yellow.100", text: "yellow.800" };
    if (rate >= 0.06) return { bg: "orange.50", text: "orange.700" };
    return { bg: "gray.50", text: "gray.500" };
  };

  const getCellColorDark = (rate: number, isMax: boolean) => {
    if (isMax) return { bg: "green.600", text: "white" };
    if (rate >= 0.2) return { bg: "green.900", text: "green.200" };
    if (rate >= 0.12) return { bg: "yellow.900", text: "yellow.200" };
    if (rate >= 0.06) return { bg: "orange.900", text: "orange.200" };
    return { bg: "whiteAlpha.50", text: "gray.500" };
  };

  const lightMode = useColorModeValue(true, false);

  return (
    <Box
      p={4}
      bg={cardBg}
      borderWidth="1px"
      borderColor={borderColor}
      borderRadius="xl"
    >
      <HStack justify="space-between" mb={4}>
        <HStack gap={2}>
          <Icon as={LuTarget} boxSize={4} color="teal.500" />
          <Text fontSize="sm" fontWeight="semibold">
            {t("htft-title")}
          </Text>
        </HStack>
        <TooltipIcon content={t("htft-info")} />
      </HStack>

      {/* Column headers */}
      <Grid templateColumns="50px repeat(3, 1fr)" gap={1.5} mb={1.5}>
        <Box />
        <Text
          fontSize="2xs"
          fontWeight="bold"
          textAlign="center"
          color="fg.muted"
        >
          {t("ft")} 1
        </Text>
        <Text
          fontSize="2xs"
          fontWeight="bold"
          textAlign="center"
          color="fg.muted"
        >
          {t("ft")} X
        </Text>
        <Text
          fontSize="2xs"
          fontWeight="bold"
          textAlign="center"
          color="fg.muted"
        >
          {t("ft")} 2
        </Text>
      </Grid>

      {/* Grid rows */}
      {HTFT_ROWS.map((row, rowIdx) => {
        const rowLabels = [`${t("ht")} 1`, `${t("ht")} X`, `${t("ht")} 2`];
        return (
          <Grid
            key={rowIdx}
            templateColumns="50px repeat(3, 1fr)"
            gap={1.5}
            mb={1.5}
          >
            <Box display="flex" alignItems="center">
              <Text fontSize="2xs" fontWeight="bold" color="fg.muted">
                {rowLabels[rowIdx]}
              </Text>
            </Box>
            {row.map((comboKey) => {
              const data = htft[comboKey] || { rate: 0, sample: 0 };
              const isMax = comboKey === maxKey && maxRate > 0.05;
              const colors = lightMode
                ? getCellColor(data.rate, isMax)
                : getCellColorDark(data.rate, isMax);

              return (
                <Box
                  key={comboKey}
                  py={2.5}
                  px={2}
                  bg={colors.bg}
                  borderRadius="lg"
                  textAlign="center"
                  position="relative"
                  transition="all 0.2s"
                  _hover={{ transform: "scale(1.04)" }}
                >
                  <Text
                    fontSize="xs"
                    fontWeight="bold"
                    color={colors.text}
                    mb={0.5}
                  >
                    {HTFT_DISPLAY[comboKey]}
                  </Text>
                  <Text
                    fontSize="lg"
                    fontWeight="extrabold"
                    color={colors.text}
                  >
                    {pct(data.rate, 0)}
                  </Text>
                  <Text
                    fontSize="2xs"
                    color={isMax ? "whiteAlpha.800" : "fg.muted"}
                  >
                    {t("matches-count", { count: data.sample })}
                  </Text>
                  {isMax && (
                    <Icon
                      as={LuBadgeCheck}
                      position="absolute"
                      top={1}
                      right={1}
                      boxSize={3.5}
                      color="white"
                    />
                  )}
                </Box>
              );
            })}
          </Grid>
        );
      })}

      {/* Best combo callout */}
      {maxRate > 0.05 && (
        <Box
          mt={2}
          p={2.5}
          bg={useColorModeValue("green.50", "green.950")}
          borderRadius="lg"
        >
          <HStack gap={2}>
            <Icon as={LuTrendingUp} boxSize={4} color="green.500" />
            <Text fontSize="xs" fontWeight="semibold">
              {t("strongest")}:{" "}
              <Text as="span" color="green.500">
                {HTFT_DISPLAY[maxKey]} ({pct(maxRate, 0)})
              </Text>
            </Text>
          </HStack>
        </Box>
      )}
    </Box>
  );
}

// ──────────────────────────────────────
// Main Panel Export
// ──────────────────────────────────────

interface V28OddsBandPanelProps {
  engine: V27EngineDto;
}

export default function V28OddsBandPanel({ engine }: V28OddsBandPanelProps) {
  const t = useTranslations("v28");
  const cardBg = useColorModeValue("white", "gray.800");
  const borderColor = useColorModeValue("gray.200", "gray.700");

  const tripleValue = engine.triple_value;
  const cards = engine.odds_band?.cards as OddsBandCardsDto | undefined;
  const htft = engine.odds_band?.htft as
    | Record<HtftComboKey, OddsBandHtftComboDto>
    | undefined;

  // Filter out HTFT triple-value entries from the main grid (shown in HTFT section)
  const mainValueEntries = Object.entries(tripleValue || {}).filter(
    ([key]) => !key.startsWith("htft_"),
  );

  // Separate value hits from non-hits for priority ordering
  const valueHits = mainValueEntries.filter(([, e]) => e.is_value);
  const valueNon = mainValueEntries.filter(([, e]) => !e.is_value);
  const orderedEntries = [...valueHits, ...valueNon];

  const hasTriple = orderedEntries.length > 0;
  const hasCards = cards && cards.sample >= 1;
  const hasHtft = htft && Object.values(htft).some((v) => v.sample > 0);

  if (!hasTriple && !hasCards && !hasHtft) return null;

  return (
    <Card.Root bg={cardBg} borderColor={borderColor} borderRadius="2xl">
      <Card.Body gap={5}>
        <SectionTitle
          icon={LuShieldAlert}
          title={t("panel-title")}
          info={t("panel-info")}
        />

        {/* Engine version badge */}
        <HStack>
          <Badge
            colorPalette="purple"
            variant="subtle"
            borderRadius="full"
            fontSize="2xs"
          >
            {engine.version}
          </Badge>
          {engine.consensus && (
            <Badge
              colorPalette={engine.consensus === "AGREE" ? "green" : "orange"}
              variant="solid"
              borderRadius="full"
              fontSize="2xs"
            >
              {engine.consensus === "AGREE"
                ? t("engines-agree")
                : t("engines-disagree")}
            </Badge>
          )}
          {valueHits.length > 0 && (
            <Badge
              colorPalette="green"
              variant="outline"
              borderRadius="full"
              fontSize="2xs"
            >
              {t("value-signals", { count: valueHits.length })}
            </Badge>
          )}
        </HStack>

        {/* Triple Value Grid */}
        {hasTriple && (
          <Box>
            <HStack mb={3} gap={2}>
              <Icon as={LuTarget} boxSize={4} color="blue.500" />
              <Text fontSize="sm" fontWeight="semibold">
                {t("triple-value-title")}
              </Text>
              <TooltipIcon content={t("triple-value-info")} />
            </HStack>
            <SimpleGrid columns={{ base: 2, md: 3, xl: 4 }} gap={2.5}>
              {orderedEntries.map(([key, entry]) => (
                <TripleValueCard
                  key={key}
                  label={TRIPLE_VALUE_KEYS.has(key) ? t(`label-${key}`) : key}
                  entry={entry}
                />
              ))}
            </SimpleGrid>
          </Box>
        )}

        {/* Cards + HTFT side by side on large screens */}
        {(hasCards || hasHtft) && (
          <Grid
            templateColumns={{
              base: "1fr",
              xl: hasCards && hasHtft ? "1fr 1fr" : "1fr",
            }}
            gap={4}
          >
            {hasCards && <CardsSection cards={cards} />}
            {hasHtft && <HtftGrid htft={htft} />}
          </Grid>
        )}
      </Card.Body>
    </Card.Root>
  );
}
