"use client";

import type { ReactNode } from "react";

import Box from "@mui/material/Box";
import Typography from "@mui/material/Typography";
import { alpha, useTheme } from "@mui/material/styles";

import { DashboardAssetIcon } from "@/components/dashboard/dashboard-asset-icon";
import { dashboardAssets } from "@/components/dashboard/dashboard-assets";
import { OfficeBuildingIcon } from "@/components/dashboard/dashboard-icons";
import { useAppLanguage } from "@/components/providers/app-language-provider";
import {
  IN_PROGRESS_COLOR,
  NOT_ADDRESSED_COLOR,
  SOLVED_COLOR,
} from "@/features/pswg/working-group/working-group-data";
import {
  normalizeWorkingGroupLanguage,
  workingGroupText,
} from "@/features/pswg/working-group/working-group-i18n";

type WorkingGroupSummaryCardProps = {
  backgroundColor: string;
  borderColor: string;
  icon: ReactNode;
  label: string;
  value: string;
};

function WorkingGroupSummaryCard({
  backgroundColor,
  borderColor,
  icon,
  label,
  value,
}: WorkingGroupSummaryCardProps) {
  const theme = useTheme();
  const isDark = theme.palette.mode === "dark";

  return (
    <Box
      sx={{
        width: "100%",
        minHeight: 96,
        px: 2,
        py: 1.75,
        borderRadius: "14px",
        bgcolor: backgroundColor,
        border: `1px solid ${borderColor}`,
      }}
    >
      <Box
        sx={{
          display: "flex",
          alignItems: "center",
          justifyContent: "space-between",
          gap: 2,
        }}
      >
        <Box sx={{ minWidth: 0 }}>
          <Typography
            sx={{
              color: isDark ? "#b8c3d6" : "#667085",
              fontSize: 11,
              fontWeight: 500,
              whiteSpace: "nowrap",
            }}
          >
            {label}
          </Typography>

          <Typography
            sx={{
              mt: 1.25,
              color: isDark ? "#f8fafc" : "#1f2937",
              fontSize: 24,
              fontWeight: 600,
              lineHeight: 1,
              letterSpacing: "-0.02em",
            }}
          >
            {value}
          </Typography>
        </Box>

        <Box
          sx={{
            width: 44,
            height: 44,
            borderRadius: "14px",
            bgcolor: isDark ? "#0b1730" : "#ffffff",
            border: `1px solid ${borderColor}`,
            display: "grid",
            placeItems: "center",
            flexShrink: 0,
          }}
        >
          {icon}
        </Box>
      </Box>
    </Box>
  );
}

export function WorkingGroupSummaryCards() {
  const theme = useTheme();
  const { language } = useAppLanguage();

  const currentLanguage = normalizeWorkingGroupLanguage(language);
  const text = workingGroupText[currentLanguage];

  const cards = [
    {
      label: text.totalIssues,
      value: "20",
      backgroundColor: alpha(
        theme.palette.primary.main,
        theme.palette.mode === "dark" ? 0.18 : 0.12
      ),
      borderColor: alpha(
        theme.palette.primary.main,
        theme.palette.mode === "dark" ? 0.35 : 0.22
      ),
      icon: <DashboardAssetIcon src={dashboardAssets.summaryTotal} size={26} />,
    },
    {
      label: text.solved,
      value: "15/20",
      backgroundColor: alpha(
        SOLVED_COLOR,
        theme.palette.mode === "dark" ? 0.18 : 0.12
      ),
      borderColor: alpha(
        SOLVED_COLOR,
        theme.palette.mode === "dark" ? 0.35 : 0.22
      ),
      icon: <DashboardAssetIcon src={dashboardAssets.summarySolved} size={26} />,
    },
    {
      label: text.inProgress,
      value: "4/20",
      backgroundColor: alpha(
        IN_PROGRESS_COLOR,
        theme.palette.mode === "dark" ? 0.22 : 0.14
      ),
      borderColor: alpha(
        IN_PROGRESS_COLOR,
        theme.palette.mode === "dark" ? 0.4 : 0.28
      ),
      icon: (
        <DashboardAssetIcon src={dashboardAssets.summaryProgress} size={26} />
      ),
    },
    {
      label: text.notAddressed,
      value: "1/20",
      backgroundColor: alpha(
        NOT_ADDRESSED_COLOR,
        theme.palette.mode === "dark" ? 0.18 : 0.12
      ),
      borderColor: alpha(
        NOT_ADDRESSED_COLOR,
        theme.palette.mode === "dark" ? 0.35 : 0.22
      ),
      icon: (
        <DashboardAssetIcon
          src={dashboardAssets.summaryNotAddressed}
          size={26}
        />
      ),
    },
    {
      label: text.totalPrimaryAgencies,
      value: "14/14",
      backgroundColor: alpha(
        theme.palette.text.secondary,
        theme.palette.mode === "dark" ? 0.12 : 0.08
      ),
      borderColor: alpha(
        theme.palette.text.secondary,
        theme.palette.mode === "dark" ? 0.24 : 0.16
      ),
      icon: (
        <OfficeBuildingIcon
          sx={{ fontSize: 28, color: theme.palette.text.secondary }}
        />
      ),
    },
  ];

  return (
    <Box
      sx={{
        display: "grid",
        gridTemplateColumns: {
          xs: "1fr",
          sm: "repeat(2, minmax(0, 1fr))",
          md: "repeat(5, minmax(0, 1fr))",
        },
        gap: 1.5,
      }}
    >
      {cards.map((card) => (
        <WorkingGroupSummaryCard key={card.label} {...card} />
      ))}
    </Box>
  );
}