"use client";

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

import FileDownloadOutlinedIcon from "@mui/icons-material/FileDownloadOutlined";
import KeyboardArrowDownIcon from "@mui/icons-material/KeyboardArrowDown";
import SearchIcon from "@mui/icons-material/Search";

import { PlusSmallIcon } from "@/components/dashboard/dashboard-icons";

import {
  cdcIssueMatrixText,
  type CdcIssueMatrixLanguage,
} from "./cdc-issue-matrix-i18n";

type CdcIssueMatrixHeaderProps = {
  search: string;
  language: CdcIssueMatrixLanguage;
  exportDisabled?: boolean;
  exportLoading?: boolean;
  onExport?: () => void;
  onSearchChange: (value: string) => void;
  onNewIssue: () => void;
};

export function CdcIssueMatrixHeader({
  search,
  language,
  exportDisabled = false,
  exportLoading = false,
  onExport,
  onSearchChange,
  onNewIssue,
}: CdcIssueMatrixHeaderProps) {
  const theme = useTheme();
  const isDark = theme.palette.mode === "dark";
  const text = cdcIssueMatrixText[language] ?? cdcIssueMatrixText.en;

  return (
    <Box
      sx={{
        mb: 3,
        display: "flex",
        alignItems: {
          xs: "stretch",
          md: "flex-start",
        },
        justifyContent: "space-between",
        gap: 2,
        flexDirection: {
          xs: "column",
          md: "row",
        },
      }}
    >
      <Box>
        <Typography
          component="h1"
          sx={{
            fontSize: {
              xs: 26,
              md: 32,
            },
            fontWeight: 700,
            color: theme.palette.text.primary,
            lineHeight: 1.15,
            mb: 0.75,
          }}
        >
          {text.title}
        </Typography>

        <Typography
          sx={{
            color: theme.palette.text.secondary,
            fontSize: 12,
            fontWeight: 500,
          }}
        >
          {text.breadcrumb}
        </Typography>
      </Box>

      <Box
        sx={{
          display: "flex",
          alignItems: "center",
          gap: 1.5,
          flexDirection: {
            xs: "column",
            sm: "row",
          },
        }}
      >
        <OutlinedInput
          size="small"
          value={search}
          onChange={(event) => onSearchChange(event.target.value)}
          placeholder={text.search}
          startAdornment={
            <InputAdornment position="start">
              <SearchIcon
                sx={{
                  color: theme.palette.primary.main,
                  fontSize: 20,
                }}
              />
            </InputAdornment>
          }
          sx={{
            width: {
              xs: "100%",
              sm: 182,
            },
            height: 40,
            borderRadius: 1.5,
            bgcolor: theme.palette.background.paper,
            color: theme.palette.primary.main,
            fontSize: 13,
            "& input::placeholder": {
              color: theme.palette.primary.main,
              opacity: 1,
            },
            "& .MuiOutlinedInput-notchedOutline": {
              borderColor: theme.palette.primary.main,
            },
            "&:hover .MuiOutlinedInput-notchedOutline": {
              borderColor: theme.palette.primary.main,
            },
            "&.Mui-focused .MuiOutlinedInput-notchedOutline": {
              borderColor: theme.palette.primary.main,
              borderWidth: 1,
            },
          }}
        />

        <Button
          variant="outlined"
          onClick={onExport}
          disabled={exportDisabled || exportLoading || !onExport}
          startIcon={<FileDownloadOutlinedIcon sx={{ fontSize: 16 }} />}
          endIcon={<KeyboardArrowDownIcon sx={{ fontSize: 18 }} />}
          sx={{
            height: 40,
            px: 1.5,
            minWidth: {
              xs: "100%",
              sm: 116,
            },
            borderRadius: 1.5,
            borderColor: theme.palette.primary.main,
            color: theme.palette.primary.main,
            bgcolor: theme.palette.background.paper,
            fontSize: 13,
            textTransform: "none",
            justifyContent: "space-between",
            "&:hover": {
              bgcolor: isDark
                ? alpha(theme.palette.primary.main, 0.12)
                : "#F0F7FF",
              borderColor: theme.palette.primary.main,
            },
          }}
        >
          {exportLoading ? "Exporting..." : text.export}
        </Button>

        <Button
          variant="contained"
          onClick={onNewIssue}
          startIcon={<PlusSmallIcon sx={{ fontSize: 18 }} />}
          sx={{
            height: 40,
            px: 1.5,
            minWidth: {
              xs: "100%",
              sm: 107,
            },
            borderRadius: "6px",
            bgcolor: theme.palette.primary.main,
            fontSize: 13,
            fontWeight: 500,
            color: "#edf8fd",
            textTransform: "none",
            boxShadow: "none",
            "& .MuiButton-startIcon": {
              mr: 0.625,
            },
            "&:hover": {
              bgcolor: theme.palette.primary.dark,
              boxShadow: "none",
            },
          }}
        >
          {text.newIssue}
        </Button>
      </Box>
    </Box>
  );
}
