import React, { useId, useState, useRef, useCallback } from 'react';
import Box from '@mui/material/Box';
import useMediaQuery from '@mui/material/useMediaQuery';
import { useTheme } from '@mui/material/styles';
import ShareOutlinedIcon from '@mui/icons-material/ShareOutlined';
import PrintOutlinedIcon from '@mui/icons-material/PrintOutlined';
import type { SxProps, Theme } from '@mui/material/styles';
import { Typography } from '../../atoms/Typography';
import { Button } from '../../atoms/Button';
import { WizardLayout } from '../../templates/WizardLayout';
import { ComparisonTable, type ComparisonPackage } from '../../organisms/ComparisonTable';
import { ComparisonPackageCard } from '../../molecules/ComparisonPackageCard';
import { ComparisonTabCard } from '../../molecules/ComparisonTabCard';

// ─── Types ───────────────────────────────────────────────────────────────────

/** Props for the ComparisonPage */
export interface ComparisonPageProps {
  /** User-selected packages to compare (max 3) */
  packages: ComparisonPackage[];
  /** System-recommended package — always shown as an additional column */
  recommendedPackage?: ComparisonPackage;
  /** Called when user clicks CTA on a package */
  onArrange: (packageId: string) => void;
  /** Called when user removes a package from comparison */
  onRemove: (packageId: string) => void;
  /** Called when user clicks Back */
  onBack: () => void;
  /** Called when user clicks Share */
  onShare?: () => void;
  /** Called when user clicks Print */
  onPrint?: () => void;
  /** Navigation bar slot */
  navigation?: React.ReactNode;
  /** MUI sx prop */
  sx?: SxProps<Theme>;
}

// ─── Component ──────────────────────────────────────────────────────────────

/**
 * Package comparison page for the FA design system (V2 — production).
 *
 * Desktop: Full ComparisonTable with info card, floating verified badges,
 * section tables with left accent borders. **Recommended package appears as
 * the first (leftmost) column.**
 * Mobile: Tabbed card view with horizontal tab rail. **Recommended package is
 * the first tab in the rail, but the first user-selected package is the
 * initially active tab** — the recommended tab is a suggestion, not the
 * default view.
 *
 * Share + Print utility actions in the page header.
 *
 * See `ComparisonPageV1.tsx` for the archived V1 (recommended-last) layout.
 */
export const ComparisonPage = React.forwardRef<HTMLDivElement, ComparisonPageProps>(
  (
    { packages, recommendedPackage, onArrange, onRemove, onBack, onShare, onPrint, navigation, sx },
    ref,
  ) => {
    const theme = useTheme();
    const isMobile = useMediaQuery(theme.breakpoints.down('md'));
    const tablistId = useId();
    const railRef = useRef<HTMLDivElement>(null);
    const tabRefs = useRef<(HTMLDivElement | null)[]>([]);

    const allPackages = React.useMemo(() => {
      const result: ComparisonPackage[] = [];
      if (recommendedPackage) {
        result.push({ ...recommendedPackage, isRecommended: true });
      }
      result.push(...packages);
      return result;
    }, [packages, recommendedPackage]);

    // On mobile, default the active tab to the first user-selected package
    // (not the recommended). Recommended is first in the rail as a suggestion.
    const defaultTabIdx = recommendedPackage ? 1 : 0;
    const [activeTabIdx, setActiveTabIdx] = useState(defaultTabIdx);
    const activePackage = allPackages[activeTabIdx] ?? allPackages[0];

    const providerCount = new Set(allPackages.map((p) => p.provider.name)).size;
    const subtitle =
      providerCount > 1
        ? `Comparing ${packages.length} package${packages.length !== 1 ? 's' : ''} from different providers`
        : `Comparing ${packages.length} package${packages.length !== 1 ? 's' : ''}`;

    const hasRecommended = allPackages.some((p) => p.isRecommended);

    const scrollToCenter = useCallback((idx: number) => {
      const tab = tabRefs.current[idx];
      if (tab && railRef.current) {
        const rail = railRef.current;
        const tabCenter = tab.offsetLeft + tab.offsetWidth / 2;
        const railCenter = rail.offsetWidth / 2;
        rail.scrollTo({ left: tabCenter - railCenter, behavior: 'smooth' });
      }
    }, []);

    const handleTabClick = useCallback(
      (idx: number) => {
        setActiveTabIdx(idx);
        scrollToCenter(idx);
      },
      [scrollToCenter],
    );

    // Center the default tab on mount
    React.useEffect(() => {
      // Small delay to allow layout to settle
      const timer = setTimeout(() => scrollToCenter(defaultTabIdx), 50);
      return () => clearTimeout(timer);
      // eslint-disable-next-line react-hooks/exhaustive-deps
    }, []);

    return (
      <Box ref={ref} sx={sx}>
        <WizardLayout
          variant="wide-form"
          navigation={navigation}
          showBackLink
          backLabel="Back"
          onBack={onBack}
        >
          {/* Page header with Share/Print actions */}
          <Box sx={{ mb: { xs: 3, md: 5 } }}>
            <Box
              sx={{
                display: 'flex',
                alignItems: 'flex-start',
                justifyContent: 'space-between',
                gap: 2,
                flexWrap: 'wrap',
              }}
            >
              <Box>
                <Typography variant="h2" component="h1" sx={{ mb: 1 }}>
                  Compare packages
                </Typography>
                <Typography variant="body1" color="text.secondary" aria-live="polite">
                  {subtitle}
                </Typography>
              </Box>

              {/* Share + Print */}
              {(onShare || onPrint) && (
                <Box sx={{ display: 'flex', gap: 1, flexShrink: 0 }}>
                  {onShare && (
                    <Button
                      variant="outlined"
                      color="secondary"
                      size="small"
                      startIcon={<ShareOutlinedIcon />}
                      onClick={onShare}
                    >
                      Share
                    </Button>
                  )}
                  {onPrint && (
                    <Button
                      variant="outlined"
                      color="secondary"
                      size="small"
                      startIcon={<PrintOutlinedIcon />}
                      onClick={onPrint}
                    >
                      Print
                    </Button>
                  )}
                </Box>
              )}
            </Box>
          </Box>

          {/* Desktop: ComparisonTable */}
          {!isMobile && (
            <ComparisonTable packages={allPackages} onArrange={onArrange} onRemove={onRemove} />
          )}

          {/* Mobile: Tab rail + card view */}
          {isMobile && allPackages.length > 0 && (
            <>
              {/* Tab rail — mini cards showing provider + package + price */}
              <Box
                ref={railRef}
                role="tablist"
                id={tablistId}
                aria-label="Packages to compare"
                sx={{
                  display: 'flex',
                  gap: 1.5,
                  overflowX: 'auto',
                  py: 2,
                  px: 2,
                  mx: -2,
                  mt: 1,
                  mb: 3,
                  scrollbarWidth: 'none',
                  '&::-webkit-scrollbar': { display: 'none' },
                  WebkitOverflowScrolling: 'touch',
                }}
              >
                {allPackages.map((pkg, idx) => (
                  <ComparisonTabCard
                    key={pkg.id}
                    ref={(el: HTMLDivElement | null) => {
                      tabRefs.current[idx] = el;
                    }}
                    pkg={pkg}
                    isActive={idx === activeTabIdx}
                    hasRecommended={hasRecommended}
                    tabId={`comparison-tab-${idx}`}
                    tabPanelId={`comparison-tabpanel-${idx}`}
                    onClick={() => handleTabClick(idx)}
                  />
                ))}
              </Box>

              {activePackage && (
                <Box
                  role="tabpanel"
                  id={`comparison-tabpanel-${activeTabIdx}`}
                  aria-labelledby={`comparison-tab-${activeTabIdx}`}
                >
                  <ComparisonPackageCard pkg={activePackage} onArrange={onArrange} />
                </Box>
              )}
            </>
          )}
        </WizardLayout>
      </Box>
    );
  },
);

ComparisonPage.displayName = 'ComparisonPage';
export default ComparisonPage;
