"use client";

import {
  Box,
  Button,
  Checkbox,
  Paper,
  Stack,
  Table,
  TableBody,
  TableCell,
  TableContainer,
  TableHead,
  TableRow,
  TextField,
  Typography,
  TablePagination,
  Modal,
} from "@mui/material";
import { useCallback, useMemo, useState, useEffect } from "react";
import { useTranslation } from "react-i18next";
import QrCodeIcon from '@mui/icons-material/QrCode';
import { GetPickOrderLineInfo } from "@/app/api/pickOrder/actions";
import { useQrCodeScannerContext } from '../QrCodeScannerProvider/QrCodeScannerProvider';
import { updateInventoryLotLineStatus } from "@/app/api/inventory/actions";
import { updateStockOutLineStatus } from "@/app/api/pickOrder/actions";
interface LotPickData {
  id: number; 
  lotId: number;
  lotNo: string;
  expiryDate: string;
  location: string;
  stockUnit: string;
  availableQty: number;
  requiredQty: number;
  actualPickQty: number;
  lotStatus: string;
  lotAvailability: 'available' | 'insufficient_stock' | 'expired' | 'status_unavailable';
  stockOutLineId?: number;
  stockOutLineStatus?: string;
  stockOutLineQty?: number;
}

interface PickQtyData {
  [lineId: number]: {
    [lotId: number]: number;
  };
}

interface LotTableProps {
  lotData: LotPickData[];
  selectedRowId: number | null;
  selectedRow: (GetPickOrderLineInfo & { pickOrderCode: string }) | null;
  pickQtyData: PickQtyData;
  selectedLotRowId: string | null;
  selectedLotId: number | null;
  onLotSelection: (uniqueLotId: string, lotId: number) => void;
  onPickQtyChange: (lineId: number, lotId: number, value: number) => void;
  onSubmitPickQty: (lineId: number, lotId: number) => void;
  onCreateStockOutLine: (inventoryLotLineId: number) => void;
  onQcCheck: (line: GetPickOrderLineInfo, pickOrderCode: string) => void;
  onLotSelectForInput: (lot: LotPickData) => void;
  showInputBody: boolean;
  setShowInputBody: (show: boolean) => void;
  selectedLotForInput: LotPickData | null;
  generateInputBody: () => any;
  onDataRefresh: () => Promise<void>;
}

// ✅ QR Code Modal Component
const QrCodeModal: React.FC<{
  open: boolean;
  onClose: () => void;
  lot: LotPickData | null;
  onQrCodeSubmit: (lotNo: string) => void;
}> = ({ open, onClose, lot, onQrCodeSubmit }) => {
  const { t } = useTranslation("pickOrder");
  const { values: qrValues, isScanning, startScan, stopScan, resetScan } = useQrCodeScannerContext();
  const [manualInput, setManualInput] = useState<string>('');
  
  // ✅ Add state to track manual input submission
  const [manualInputSubmitted, setManualInputSubmitted] = useState<boolean>(false);
  const [manualInputError, setManualInputError] = useState<boolean>(false);

  // ✅ Process scanned QR codes
  useEffect(() => {
    if (qrValues.length > 0 && lot) {
      const latestQr = qrValues[qrValues.length - 1];
      const qrContent = latestQr.replace(/[{}]/g, '');
      
      if (qrContent === lot.lotNo) {
        onQrCodeSubmit(lot.lotNo);
        onClose();
        resetScan();
      } else {
        // ✅ Set error state for helper text
        setManualInputError(true);
        setManualInputSubmitted(true);
      }
    }
  }, [qrValues, lot, onQrCodeSubmit, onClose, resetScan]);

  // ✅ Clear states when modal opens or lot changes
  useEffect(() => {
    if (open) {
      setManualInput('');
      setManualInputSubmitted(false);
      setManualInputError(false);
    }
  }, [open]);

  useEffect(() => {
    if (lot) {
      setManualInput('');
      setManualInputSubmitted(false);
      setManualInputError(false);
    }
  }, [lot]);

{/* 
const handleManualSubmit = () => {
    if (manualInput.trim() === lot?.lotNo) {
      // ✅ Success - no error helper text needed
      onQrCodeSubmit(lot.lotNo);
      onClose();
      setManualInput('');
    } else {
      // ✅ Show error helper text after submit
      setManualInputError(true);
      setManualInputSubmitted(true);
      // Don't clear input - let user see what they typed
    }
  };

  return (
    <Modal open={open} onClose={onClose}>
      <Box sx={{
        position: 'absolute',
        top: '50%',
        left: '50%',
        transform: 'translate(-50%, -50%)',
        bgcolor: 'background.paper',
        p: 3,
        borderRadius: 2,
        minWidth: 400,
      }}>
        <Typography variant="h6" gutterBottom>
          {t("QR Code Scan for Lot")}: {lot?.lotNo}
        </Typography>
       
        <Box sx={{ mb: 2, p: 2, backgroundColor: '#f5f5f5', borderRadius: 1 }}>
          <Typography variant="body2" gutterBottom>
            <strong>Scanner Status:</strong> {isScanning ? 'Scanning...' : 'Ready'}
          </Typography>
          <Stack direction="row" spacing={1}>
            <Button
              variant="contained"
              onClick={isScanning ? stopScan : startScan}
              size="small"
            >
              {isScanning ? 'Stop Scan' : 'Start Scan'}
            </Button>
            <Button
              variant="outlined"
              onClick={resetScan}
              size="small"
            >
              Reset
            </Button>
          </Stack>
        </Box>

   
        <Box sx={{ mb: 2 }}>
          <Typography variant="body2" gutterBottom>
            <strong>Manual Input:</strong>
          </Typography>
          <TextField
            fullWidth
            size="small"
            value={manualInput}
            onChange={(e) => setManualInput(e.target.value)}
            sx={{ mb: 1 }}
            // ✅ Only show error after submit button is clicked
            error={manualInputSubmitted && manualInputError}
            helperText={
              // ✅ Show helper text only after submit with error
              manualInputSubmitted && manualInputError
                ? `The input is not the same as the expected lot number. Expected: ${lot?.lotNo}`
                : ''
            }
          />
          <Button
            variant="contained"
            onClick={handleManualSubmit}
            disabled={!manualInput.trim()}
            size="small"
            color="primary"
          >
            Submit Manual Input
          </Button>
        </Box>

        {qrValues.length > 0 && (
          <Box sx={{ mb: 2, p: 2, backgroundColor: manualInputError ? '#ffebee' : '#e8f5e8', borderRadius: 1 }}>
            <Typography variant="body2" color={manualInputError ? 'error' : 'success'}>
              <strong>QR Scan Result:</strong> {qrValues[qrValues.length - 1]}
            </Typography>
            {manualInputError && (
              <Typography variant="caption" color="error" display="block">
                ❌ Mismatch! Expected: {lot?.lotNo}
              </Typography>
            )}
          </Box>
        )}

        <Box sx={{ mt: 2, textAlign: 'right' }}>
          <Button onClick={onClose} variant="outlined">
            Cancel
          </Button>
        </Box>
      </Box>
    </Modal>
  );
};
*/}
useEffect(() => {
  if (manualInput.trim() === lot?.lotNo && manualInput.trim() !== '') {
    // Auto-submit when manual input matches the expected lot number
    console.log('🔄 Auto-submitting manual input:', manualInput.trim());
    
    // Add a small delay to ensure proper execution order
    const timer = setTimeout(() => {
      onQrCodeSubmit(lot.lotNo);
      onClose();
      setManualInput('');
      setManualInputError(false);
      setManualInputSubmitted(false);
    }, 200); // 200ms delay
    
    return () => clearTimeout(timer);
  }
}, [manualInput, lot, onQrCodeSubmit, onClose]);
  const handleManualSubmit = () => {
    if (manualInput.trim() === lot?.lotNo) {
      // ✅ Success - no error helper text needed
      onQrCodeSubmit(lot.lotNo);
      onClose();
      setManualInput('');
    } else {
      // ✅ Show error helper text after submit
      setManualInputError(true);
      setManualInputSubmitted(true);
      // Don't clear input - let user see what they typed
    }
  };
useEffect(() => {
    if (open) {
      startScan();
    }
  }, [open, startScan]);
  return (
    <Modal open={open} onClose={onClose}>
      <Box sx={{
        position: 'absolute',
        top: '50%',
        left: '50%',
        transform: 'translate(-50%, -50%)',
        bgcolor: 'background.paper',
        p: 3,
        borderRadius: 2,
        minWidth: 400,
      }}>
        <Typography variant="h6" gutterBottom>
          QR Code Scan for Lot: {lot?.lotNo}
        </Typography>
        
        {/* Manual Input with Submit-Triggered Helper Text */}
        <Box sx={{ mb: 2 }}>
          <Typography variant="body2" gutterBottom>
            <strong>Manual Input:</strong>
          </Typography>
          <TextField
            fullWidth
            size="small"
            value={manualInput}
            onChange={(e) => setManualInput(e.target.value)}
            sx={{ mb: 1 }}
            error={manualInputSubmitted && manualInputError}
            helperText={
              manualInputSubmitted && manualInputError
                ? `The input is not the same as the expected lot number.`
                : ''
            }
          />
          <Button
            variant="contained"
            onClick={handleManualSubmit}
            disabled={!manualInput.trim()}
            size="small"
            color="primary"
          >
            Submit Manual Input
          </Button>
        </Box>

        {/* Show QR Scan Status */}
        {qrValues.length > 0 && (
          <Box sx={{ mb: 2, p: 2, backgroundColor: manualInputError ? '#ffebee' : '#e8f5e8', borderRadius: 1 }}>
            <Typography variant="body2" color={manualInputError ? 'error' : 'success'}>
              <strong>QR Scan Result:</strong> {qrValues[qrValues.length - 1]}
            </Typography>
            {manualInputError && (
              <Typography variant="caption" color="error" display="block">
                ❌ Mismatch! Expected!
              </Typography>
            )}
          </Box>
        )}

        <Box sx={{ mt: 2, textAlign: 'right' }}>
          <Button onClick={onClose} variant="outlined">
            Cancel
          </Button>
        </Box>
      </Box>
    </Modal>
  );
};


const LotTable: React.FC<LotTableProps> = ({
  lotData,
  selectedRowId,
  selectedRow,
  pickQtyData,
  selectedLotRowId,
  selectedLotId,
  onLotSelection,
  onPickQtyChange,
  onSubmitPickQty,
  onCreateStockOutLine,
  onQcCheck,
  onLotSelectForInput,
  showInputBody,
  setShowInputBody,
  selectedLotForInput,
  generateInputBody,
  onDataRefresh,
}) => {
  const { t } = useTranslation("pickOrder");
  
  // ✅ Add QR scanner context
  const { values: qrValues, isScanning, startScan, stopScan, resetScan } = useQrCodeScannerContext();
  
  // ✅ Add state for QR input modal
  const [qrModalOpen, setQrModalOpen] = useState(false);
  const [selectedLotForQr, setSelectedLotForQr] = useState<LotPickData | null>(null);
  const [manualQrInput, setManualQrInput] = useState<string>('');
  
  // 分页控制器
  const [lotTablePagingController, setLotTablePagingController] = useState({
    pageNum: 0,
    pageSize: 10,
  });

  // ✅ 添加状态消息生成函数
  const getStatusMessage = useCallback((lot: LotPickData) => {
    if (!lot.stockOutLineId) {
      return t("Please finish QR code scan, QC check and pick order.");
    }
    
    switch (lot.stockOutLineStatus?.toLowerCase()) {
      case 'pending':
        return t("Please submit pick order.");
      case 'checked':
        return t("Please submit the pick order.");
      case 'partially_completed':
        return t("Partial quantity submitted. Please submit more or complete the order.") ;
      case 'completed':
        return t("Pick order completed successfully!");
      case 'rejected':
        return t("QC check failed. Lot has been rejected and marked as unavailable.");
      case 'unavailable':
        return t("This order is insufficient, please pick another lot.");
      default:
        return t("Please finish QR code scan, QC check and pick order.");
    }
  }, []);

  const prepareLotTableData = useMemo(() => {
    return lotData.map((lot) => ({
      ...lot,
      id: lot.lotId,
    }));
  }, [lotData]);

  // 分页数据
  const paginatedLotTableData = useMemo(() => {
    const startIndex = lotTablePagingController.pageNum * lotTablePagingController.pageSize;
    const endIndex = startIndex + lotTablePagingController.pageSize;
    return prepareLotTableData.slice(startIndex, endIndex);
  }, [prepareLotTableData, lotTablePagingController]);

  // 分页处理函数
  const handleLotTablePageChange = useCallback((event: unknown, newPage: number) => {
    setLotTablePagingController(prev => ({
      ...prev,
      pageNum: newPage,
    }));
  }, []);

  const handleLotTablePageSizeChange = useCallback((event: React.ChangeEvent<HTMLInputElement>) => {
    const newPageSize = parseInt(event.target.value, 10);
    setLotTablePagingController({
      pageNum: 0,
      pageSize: newPageSize,
    });
  }, []);

  // ✅ Handle QR code submission
  const handleQrCodeSubmit = useCallback(async (lotNo: string) => {
    if (selectedLotForQr && selectedLotForQr.lotNo === lotNo) {
      console.log(`✅ QR Code verified for lot: ${lotNo}`);
      
      // ✅ Store the required quantity before creating stock out line
      const requiredQty = selectedLotForQr.requiredQty;
      const lotId = selectedLotForQr.lotId;
      
      // ✅ Create stock out line and wait for it to complete
      await onCreateStockOutLine(selectedLotForQr.lotId);
      
      // ✅ Close modal
      setQrModalOpen(false);
      setSelectedLotForQr(null);
      
      // ✅ Set pick quantity AFTER stock out line creation and refresh is complete
      if (selectedRowId) {
        // Add a small delay to ensure the data refresh from onCreateStockOutLine is complete
        setTimeout(() => {
          onPickQtyChange(selectedRowId, lotId, requiredQty);
          console.log(`✅ Auto-set pick quantity to ${requiredQty} for lot ${lotNo}`);
        }, 500); // 500ms delay to ensure refresh is complete
      }
      
      // ✅ Show success message
      console.log("Stock out line created successfully!");
    }
  }, [selectedLotForQr, onCreateStockOutLine, selectedRowId, onPickQtyChange]);

  return (
    <>
      <TableContainer component={Paper}>
        <Table>
          <TableHead>
            <TableRow>
              <TableCell>{t("Selected")}</TableCell>
              <TableCell>{t("Lot#")}</TableCell>
              <TableCell>{t("Lot Expiry Date")}</TableCell>
              <TableCell>{t("Lot Location")}</TableCell>
              <TableCell align="right">{t("Available Lot")}</TableCell>
              <TableCell align="right">{t("Lot Required Pick Qty")}</TableCell>
              <TableCell>{t("Stock Unit")}</TableCell>
              <TableCell align="center">{t("QR Code Scan")}</TableCell>
              <TableCell align="center">{t("QC Check")}</TableCell>
              <TableCell align="right">{t("Lot Actual Pick Qty")}</TableCell>
              <TableCell align="center">{t("Submit")}</TableCell>
            </TableRow>
          </TableHead>
          <TableBody>
            {paginatedLotTableData.length === 0 ? (
              <TableRow>
                <TableCell colSpan={11} align="center">
                  <Typography variant="body2" color="text.secondary">
                    {t("No data available")}
                  </Typography>
                </TableCell>
              </TableRow>
            ) : (
              paginatedLotTableData.map((lot, index) => (
                <TableRow key={lot.id}>
                  <TableCell>
                    <Checkbox
                      checked={selectedLotRowId === `row_${index}`}
                      onChange={() => onLotSelection(`row_${index}`, lot.lotId)}
                      // ✅ Allow selection of available AND insufficient_stock lots
                      //disabled={lot.lotAvailability === 'expired' || lot.lotAvailability === 'status_unavailable'}
                      value={`row_${index}`}
                      name="lot-selection"
                    />
                  </TableCell>
                  <TableCell>
                    <Box>
                      <Typography>{lot.lotNo}</Typography>
                      {lot.lotAvailability !== 'available' && (
                        <Typography variant="caption" color="error" display="block">
                          ({lot.lotAvailability === 'expired' ? 'Expired' : 
                            lot.lotAvailability === 'insufficient_stock' ? 'Insufficient' : 
                            'Unavailable'})
                        </Typography>
                      )}
                    </Box>
                  </TableCell>
                  <TableCell>{lot.expiryDate}</TableCell>
                  <TableCell>{lot.location}</TableCell>
                  <TableCell align="right">{lot.availableQty.toLocaleString()}</TableCell>
                  <TableCell align="right">{lot.requiredQty.toLocaleString()}</TableCell>
                  <TableCell>{lot.stockUnit}</TableCell>
                  
                  {/* QR Code Scan Button */}
                  <TableCell align="center">
                    <Box sx={{ textAlign: 'center' }}>
                      <Button
                        variant="outlined"
                        size="small"
                        onClick={() => {
                          setSelectedLotForQr(lot);
                          setQrModalOpen(true);
                          resetScan();
                        }}
                        // ✅ Disable when:
                        // 1. Lot is expired or unavailable
                        // 2. Already scanned (has stockOutLineId)
                        // 3. Not selected (selectedLotRowId doesn't match)
                        disabled={
                          (lot.lotAvailability === 'expired' || lot.lotAvailability === 'status_unavailable') || 
                          Boolean(lot.stockOutLineId) ||
                          selectedLotRowId !== `row_${index}`
                        }
                        sx={{ 
                          fontSize: '0.7rem',
                          py: 0.5,
                          minHeight: '28px',
                          whiteSpace: 'nowrap',
                          minWidth: '40px',
                          // ✅ Visual feedback
                          opacity: selectedLotRowId === `row_${index}` ? 1 : 0.5
                        }}
                        startIcon={<QrCodeIcon />}
                        title={
                          selectedLotRowId !== `row_${index}` 
                            ? "Please select this lot first to enable QR scanning"
                            : lot.stockOutLineId 
                              ? "Already scanned"
                              : "Click to scan QR code"
                        }
                      >
                        {lot.stockOutLineId ? t("Scanned") : t("Scan")}
                      </Button>
                      
               
                    </Box>
                  </TableCell>
                  
                  {/* QC Check Button */}
                  {/*
                  <TableCell align="center">
                    <Button
                      variant="outlined"
                      size="small"
                      onClick={() => {
                        if (selectedRowId && selectedRow) {
                          onQcCheck(selectedRow, selectedRow.pickOrderCode);
                        }
                      }}
                      // ✅ Enable QC check only when stock out line exists
                      disabled={!lot.stockOutLineId || selectedLotRowId !== `row_${index}`}
                      sx={{ 
                        fontSize: '0.7rem',
                        py: 0.5,
                        minHeight: '28px',
                        whiteSpace: 'nowrap',
                        minWidth: '40px'
                      }}
                    >
                      {t("QC")}
                    </Button>
                    */}
              
                  
                  {/* Lot Actual Pick Qty */}
                  <TableCell align="right">
                  <TextField
                    type="number"
                    value={selectedRowId ? (pickQtyData[selectedRowId]?.[lot.lotId] || '') : ''} // ✅ Fixed: Use empty string instead of 0
                    onChange={(e) => {
                      if (selectedRowId) {
                        const inputValue = e.target.value;
                        // ✅ Fixed: Handle empty string and prevent leading zeros
                        if (inputValue === '') {
                          // Allow empty input (user can backspace to clear)
                          onPickQtyChange(selectedRowId, lot.lotId, 0);
                        } else {
                          // Parse the number and prevent leading zeros
                          const numValue = parseInt(inputValue, 10);
                          if (!isNaN(numValue)) {
                            onPickQtyChange(selectedRowId, lot.lotId, numValue);
                          }
                        }
                      }
                    }}
                    onBlur={(e) => {
                      // ✅ Fixed: When input loses focus, ensure we have a valid number
                      if (selectedRowId) {
                        const currentValue = pickQtyData[selectedRowId]?.[lot.lotId];
                        if (currentValue === undefined || currentValue === null) {
                          // Set to 0 if no value
                          onPickQtyChange(selectedRowId, lot.lotId, 0);
                        }
                      }
                    }}
                    inputProps={{ 
                      min: 0, 
                      max: lot.availableQty,
                      step: 1 // Allow only whole numbers
                    }}
                    // ✅ Allow input for available AND insufficient_stock lots
                    disabled={lot.lotAvailability === 'expired' || lot.lotAvailability === 'status_unavailable'}
                    sx={{ width: '80px' }}
                    placeholder="0" // Show placeholder instead of default value
                  />
                </TableCell>
                <TableCell align="center">
                <Button
                variant="outlined"
                size="small"
                onClick={async () => {
                  if (selectedRowId && selectedRow && lot.stockOutLineId) {
                    try {
                      // ✅ Call updateStockOutLineStatus to reject the stock out line
                      await updateStockOutLineStatus({
                        id: lot.stockOutLineId,
                        status: 'rejected',
                        qty: 0
                      });
                      
                      // ✅ Refresh data after rejection
                      if (onDataRefresh) {
                        await onDataRefresh();
                      }
                    } catch (error) {
                      console.error("Error rejecting lot:", error);
                    }
                  }
                }}
                // ✅ Only enable if stock out line exists
                disabled={!lot.stockOutLineId}
                sx={{ 
                  fontSize: '0.7rem',
                  py: 0.5,
                  minHeight: '28px',
                  whiteSpace: 'nowrap',
                  minWidth: '40px'
                }}
              >
                {t("Reject")}
              </Button>
                  </TableCell>
                  {/* Submit Button */}
                  <TableCell align="center">
                    <Button
                      variant="contained"
                      onClick={() => {
                        if (selectedRowId) {
                          onSubmitPickQty(selectedRowId, lot.lotId);
                        }
                        
                      }}
                      disabled={
                        (lot.lotAvailability === 'expired' || lot.lotAvailability === 'status_unavailable') || 
                        !pickQtyData[selectedRowId!]?.[lot.lotId] ||
                        !lot.stockOutLineStatus || // Must have stock out line
                        !['pending','checked', 'partially_completed'].includes(lot.stockOutLineStatus.toLowerCase()) // Only these statuses
                      }
                      // ✅ Allow submission for available AND insufficient_stock lots
                      sx={{ 
                        fontSize: '0.75rem',
                        py: 0.5,
                        minHeight: '28px'
                      }}
                    >
                      {t("Submit")}
                    </Button>
                  </TableCell>
                </TableRow>
              ))
            )}
          </TableBody>
        </Table>
      </TableContainer>
      
      {/* ✅ Status Messages Display */}
      {paginatedLotTableData.length > 0 && (
        <Box sx={{ mt: 2, p: 2, backgroundColor: 'grey.50', borderRadius: 1 }}>
          {paginatedLotTableData.map((lot, index) => (
            <Box key={lot.id} sx={{ mb: 1 }}>
              <Typography variant="body2" color="text.secondary">
                <strong>Lot {lot.lotNo}:</strong> {getStatusMessage(lot)}
              </Typography>
            </Box>
          ))}
        </Box>
      )}
      

      
      <TablePagination
        component="div"
        count={prepareLotTableData.length}
        page={lotTablePagingController.pageNum}
        rowsPerPage={lotTablePagingController.pageSize}
        onPageChange={handleLotTablePageChange}
        onRowsPerPageChange={handleLotTablePageSizeChange}
        rowsPerPageOptions={[10, 25, 50]}
        labelRowsPerPage={t("Rows per page")}
        labelDisplayedRows={({ from, to, count }) => 
          `${from}-${to} of ${count !== -1 ? count : `more than ${to}`}`
        }
      />
      
      {/* ✅ QR Code Modal */}
      <QrCodeModal
        open={qrModalOpen}
        onClose={() => {
          setQrModalOpen(false);
          setSelectedLotForQr(null);
          stopScan();
          resetScan();
        }}
        lot={selectedLotForQr}
        onQrCodeSubmit={handleQrCodeSubmit}
      />
    </>
  );
};

export default LotTable;