"use client";

import {
  FormControl,
  InputLabel,
  Select,
  MenuItem,
  OutlinedInput,
  Box,
  SelectChangeEvent,
  Checkbox,
  ListItemText,
  IconButton,
  InputAdornment,
  Tooltip,
} from "@mui/material";
import ClearIcon from "@mui/icons-material/Clear";

interface CategoryFilterProps {
  categories: string[];
  selected: string[];
  onChange: (selected: string[]) => void;
}

const CategoryFilter: React.FC<CategoryFilterProps> = ({
  categories,
  selected,
  onChange,
}) => {
  const handleChange = (event: SelectChangeEvent<string[]>) => {
    const value = event.target.value;
    onChange(typeof value === "string" ? value.split(",") : value);
  };

  return (
    <FormControl
      size="small"
      sx={{
        minWidth: { xs: "100%", sm: 260 },
        maxWidth: { xs: "100%", sm: 400 },
        "& .MuiOutlinedInput-root": {
          borderRadius: "12px",
          backgroundColor: "#fff",
          "&:hover .MuiOutlinedInput-notchedOutline": {
            borderColor: "#6c63ff",
          },
          "&.Mui-focused .MuiOutlinedInput-notchedOutline": {
            borderColor: "#6c63ff",
          },
        },
        "& .MuiInputLabel-root.Mui-focused": {
          color: "#9c98f6ff",
        },
      }}
    >
      <InputLabel id="category-filter-label">Filter by Subject</InputLabel>
      <Select
        labelId="category-filter-label"
        id="category-filter"
        multiple
        value={selected}
        onChange={handleChange}
        input={<OutlinedInput label="Filter by Category" />}
        renderValue={(sel) => (
          <span style={{ color: "#6c63ff", fontWeight: 600, fontSize: 14 }}>
            {sel.length} Subjects Selected
          </span>
        )}
        endAdornment={
          selected.length > 0 ? (
            <InputAdornment position="end" sx={{ mr: 2 }}>
              <Tooltip title="Clear All">
                <IconButton
                  size="small"
                  onClick={(e) => {
                    e.stopPropagation();
                    onChange([]);
                  }}
                  sx={{ color: "#6c63ff" }}
                >
                  <ClearIcon fontSize="small" />
                </IconButton>
              </Tooltip>
            </InputAdornment>
          ) : null
        }
        MenuProps={{
          PaperProps: {
            sx: {
              borderRadius: "12px",
              mt: 0.5,
              maxHeight: 300,
            },
          },
        }}
      >
        {categories.map((cat) => (
          <MenuItem key={cat} value={cat}>
            <Checkbox
              checked={selected.includes(cat)}
              sx={{
                color: "#6c63ff",
                "&.Mui-checked": { color: "#6c63ff" },
              }}
            />
            <ListItemText primary={cat} />
          </MenuItem>
        ))}
      </Select>
    </FormControl>
  );
};

export default CategoryFilter;
