"use client";
import React, { useCallback, useEffect, useMemo, useState } from "react";
import {
  Box,
  Button,
  Card,
  CardContent,
  CardActions,
  Stack,
  Typography,
  Chip,
  CircularProgress,
  Grid,
} from "@mui/material";
import ArrowBackIcon from "@mui/icons-material/ArrowBack";
import { useTranslation } from "react-i18next";
import { fetchAllJoPickOrders, AllJoPickOrderResponse } from "@/app/api/jo/actions";
import JobPickExecution from "./newJobPickExecution";
import SearchBox, { Criterion } from "../SearchBox";
import dayjs from "dayjs";
import type { PrinterCombo } from "@/app/api/settings/printer";

interface Props {
  /** Reserved for tabs parity with Jodetail; not used in workbench list yet. */
  onSwitchToRecordTab?: () => void;
  printerCombo: PrinterCombo[];
}

/** Jo workbench: same list + detail flow as Jodetail `JoPickOrderList`, detail uses `JoWorkbench/newJobPickExecution`. */
const JoPickOrderList: React.FC<Props> = ({ printerCombo }) => {
  const { t } = useTranslation(["common", "jo"]);
  const today = dayjs().format("YYYY-MM-DD");
  const [loading, setLoading] = useState(false);
  const [pickOrders, setPickOrders] = useState<AllJoPickOrderResponse[]>([]);
  const [selectedPickOrderId, setSelectedPickOrderId] = useState<number | undefined>(undefined);
  const [selectedJobOrderId, setSelectedJobOrderId] = useState<number | undefined>(undefined);
  const [searchQuery, setSearchQuery] = useState<Record<string, any>>({ planStart: today });

  type FloorFilter = "ALL" | "2F" | "3F" | "4F" | "NO_LOT";

  const searchCriteria: Criterion<any>[] = [
    { label: t("Job Order Code"), paramName: "jobOrderCode", type: "text" },
    { label: t("Pick Order"), paramName: "pickOrderCode", type: "text" },
    { label: t("Item Name"), paramName: "itemName", type: "text" },
    {
      label: t("Job Order Type"),
      paramName: "BOM Description",
      type: "select-labelled",
      options: [
        //{ label: t("All"), value: "All" },
        { label: t("FG"), value: "FG" },
        { label: t("WIP"), value: "WIP" },
      ],
    },
    { label: t("Plan Start"), paramName: "planStart", type: "date" },
    {
      label: t("BOM Type"),
      paramName: "bomType",
      type: "select-labelled",
      options: [
        //{ label: t("All"), value: "All" },
        { label: t("Drink"), value: "drink" },
        { label: t("Powder Mixture"), value: "Powder_Mixture" },
        { label: t("Other"), value: "other" },
      ],
    },
    {
      label: t("Floor"),
      paramName: "floor",
      type: "select-labelled",
      options: [
        //{ label: t("All"), value: "ALL" },
        { label: "2F", value: "2F" },
        { label: "3F", value: "3F" },
        { label: "4F", value: "4F" },
        { label: t("No Lot"), value: "NO_LOT" },
      ],
    },
  ];

  const selectedFloor: FloorFilter = (() => {
    const floor = String(searchQuery.floor || "ALL");
    if (floor === "2F" || floor === "3F" || floor === "4F" || floor === "NO_LOT") {
      return floor;
    }
    return "ALL";
  })();

  const fetchPickOrders = useCallback(async (query?: Record<string, any>) => {
    setLoading(true);
    try {
      const currentQuery = query ?? { planStart: today };
      const bomTypeValue = String(currentQuery.bomType || "All");
      const floorValue = String(currentQuery.floor || "ALL");
      const isAllOption = (value: string) => {
        const normalized = value.trim().toLowerCase();
        return normalized === "" || normalized === "all";
      };
      const typeParam = isAllOption(bomTypeValue) ? undefined : bomTypeValue;
      const floorParam = isAllOption(floorValue) ? undefined : floorValue;
      const jobOrderCode = String(currentQuery.jobOrderCode || "").trim();
      const pickOrderCode = String(currentQuery.pickOrderCode || "").trim();
      const itemName = String(currentQuery.itemName || "").trim();
      const bomDescription = String(currentQuery.bomDescription || "").trim();
      const planStart = String(currentQuery.planStart || "").trim();

      const data = await fetchAllJoPickOrders(typeParam, floorParam, {
        jobOrderCode: jobOrderCode || undefined,
        pickOrderCode: pickOrderCode || undefined,
        itemName: itemName || undefined,
        bomDescription: isAllOption(bomDescription) ? undefined : bomDescription,
        planStart: planStart || undefined,
      });
      setPickOrders(Array.isArray(data) ? data : []);
    } catch (e) {
      console.error(e);
      setPickOrders([]);
    } finally {
      setLoading(false);
    }
  }, [today]);

  const handleSearch = useCallback((query: Record<string, any>) => {
    const nextQuery = { ...query };
    setSearchQuery(nextQuery);
    void fetchPickOrders(nextQuery);
  }, [fetchPickOrders]);

  const handleSearchReset = useCallback(() => {
    const resetQuery = {};
    setSearchQuery(resetQuery);
    void fetchPickOrders(resetQuery);
  }, [fetchPickOrders]);

  useEffect(() => {
    void fetchPickOrders({ planStart: today });
  }, [fetchPickOrders, today]);
  const handleBackToList = useCallback(() => {
    setSelectedPickOrderId(undefined);
    setSelectedJobOrderId(undefined);
    void fetchPickOrders(searchQuery);
  }, [fetchPickOrders, searchQuery]);
  if (selectedPickOrderId !== undefined) {
    return (
      <Box>
        <Box sx={{ mb: 2 }}>
          <Button
            variant="outlined"
            onClick={handleBackToList}
            startIcon={<ArrowBackIcon />}
          >
            {t("Back to List")}
          </Button>
        </Box>
        <JobPickExecution
          filterArgs={{ pickOrderId: selectedPickOrderId, jobOrderId: selectedJobOrderId }}
          onBackToList={handleBackToList}
          printerCombo={printerCombo}
        />
      </Box>
    );
  }

  return (
    <Box>
      <Box sx={{ mb: 2 }}>
        <SearchBox
          criteria={searchCriteria}
          onSearch={handleSearch}
          onReset={handleSearchReset}
        />
      </Box>
      {loading ? (
        <Box sx={{ display: "flex", justifyContent: "center", p: 3 }}>
          <CircularProgress />
        </Box>
      ) : (
        <Box>
          <Typography variant="body2" color="text.secondary" sx={{ mb: 2 }}>
            {t("Total pick orders")}: {pickOrders.length}
          </Typography>
 
          <Grid container spacing={2}>
            {pickOrders.map((pickOrder) => {
              const status = String(pickOrder.jobOrderStatus || "");
              const statusLower = status.toLowerCase();
              const statusColor =
                statusLower === "completed"
                  ? "success"
                  : statusLower === "pending" || statusLower === "processing"
                    ? "primary"
                    : "default";
 
              const finishedCount = pickOrder.finishedPickOLineCount ?? 0;
 
              return (
                <Grid key={pickOrder.id} item xs={12} sm={6} md={4}>
                  <Card
                    sx={{
                      minHeight: 180,
                      maxHeight: 280,
                      display: "flex",
                      flexDirection: "column",
                    }}
                  >
                    <CardContent
                      sx={{
                        pb: 1,
                        flexGrow: 1,
                        overflow: "auto",
                      }}
                    >
                      <Stack direction="row" justifyContent="space-between" alignItems="center">
                        <Box sx={{ minWidth: 0 }}>
                          <Typography variant="subtitle1">
                            {t("Job Order")}: {pickOrder.jobOrderCode || "-"}
                          </Typography>
                        </Box>
                        <Chip size="small" label={t(status)} color={statusColor as any} />
                      </Stack>
                      <Typography variant="body2" color="text.secondary">
                        {t("Lot No")}: {pickOrder.lotNo || "-"}
                      </Typography>
                      <Typography variant="body2" color="text.secondary">
                        {t("Pick Order")}: {pickOrder.pickOrderCode || "-"}
                      </Typography>
                      <Typography variant="body2" color="text.secondary">
                        {t("Item Name")}: {pickOrder.itemName}
                        {pickOrder.bomDescription ? ` (${t(pickOrder.bomDescription)})` : ""}
                      </Typography>
                      <Typography variant="body2" color="text.secondary">
                        {t("Required Qty")}: {pickOrder.reqQty} ({pickOrder.uomName})
                      </Typography>
                      {selectedFloor === "ALL" ? (
                        <>
                          {pickOrder.floorPickCounts?.map(({ floor, finishedCount, totalCount }) => (
                            <Typography
                              key={floor}
                              variant="body2"
                              color="text.secondary"
                              component="span"
                              sx={{ mr: 1 }}
                            >
                              {floor}: {finishedCount}/{totalCount}
                            </Typography>
                          ))}
                          {!!pickOrder.noLotPickCount && (
                            <Typography
                              key="NO_LOT"
                              variant="body2"
                              color="text.secondary"
                              component="span"
                              sx={{ mr: 1 }}
                            >
                              {t("No Lot")}: {pickOrder.noLotPickCount.finishedCount}/{pickOrder.noLotPickCount.totalCount}
                            </Typography>
                          )}
                        </>
                      ) : selectedFloor === "NO_LOT" ? (
                        !!pickOrder.noLotPickCount && (
                          <Typography
                            key="NO_LOT"
                            variant="body2"
                            color="text.secondary"
                            component="span"
                            sx={{ mr: 1 }}
                          >
                            {t("No Lot")}: {pickOrder.noLotPickCount.finishedCount}/{pickOrder.noLotPickCount.totalCount}
                          </Typography>
                        )
                      ) : (
                        pickOrder.floorPickCounts
                          ?.filter((c) => c.floor === selectedFloor)
                          .map(({ floor, finishedCount, totalCount }) => (
                            <Typography
                              key={floor}
                              variant="body2"
                              color="text.secondary"
                              component="span"
                              sx={{ mr: 1 }}
                            >
                              {floor}: {finishedCount}/{totalCount}
                            </Typography>
                          ))
                      )}
                      {typeof pickOrder.suggestedFailCount === "number" && pickOrder.suggestedFailCount > 0 && (
                        <Typography variant="body2" color="error" sx={{ mt: 0.5 }}>
                          {t("Suggested Fail")}: {pickOrder.suggestedFailCount}
                        </Typography>
                      )}
                      {statusLower !== "pending" && finishedCount > 0 && (
                        <Box sx={{ mt: 1 }}>
                          <Typography variant="body2" fontWeight={600}>
                            {t("Finished lines")}: {finishedCount}
                          </Typography>
                        </Box>
                      )}
                    </CardContent>
 
                    <CardActions sx={{ pt: 0.5 }}>
                      <Button
                        variant="contained"
                        size="small"
                        onClick={() => {
                          setSelectedPickOrderId(pickOrder.pickOrderId ?? undefined);
                          setSelectedJobOrderId(pickOrder.jobOrderId ?? undefined);
                        }}
                      >
                        {t("View Details")}
                      </Button>
                      <Box sx={{ flex: 1 }} />
                    </CardActions>
                  </Card>
                </Grid>
              );
            })}
          </Grid>
        </Box>
      )}
    </Box>
  );
};

export default JoPickOrderList;
