import { useEffect, useState } from 'react';
import { motion } from 'framer-motion';
import { getOrderDetails } from '../api/checkoutClient';
import { OrderDetailsResponse } from '../types';
import { CarbonImpactComparison } from '../components/CarbonImpactComparison';
import { useCalculatorState } from '../hooks/useCalculatorState';

interface CheckoutSuccessProps {
  onNavigateHome: () => void;
  onNavigateCalculator: () => void;
}

// Map backend status to user-friendly labels
const getStatusDisplay = (status: string): { label: string; className: string } => {
  switch (status) {
    case 'paid':
    case 'fulfilled':
      return { label: 'Confirmed', className: 'bg-green-100 text-green-700' };
    case 'pending':
      return { label: 'Processing', className: 'bg-yellow-100 text-yellow-700' };
    default:
      return { label: status.toUpperCase(), className: 'bg-slate-100 text-slate-700' };
  }
};

// Format currency with commas
const formatCurrency = (amount: number): string => {
  return amount.toLocaleString('en-US', {
    minimumFractionDigits: 2,
    maximumFractionDigits: 2
  });
};

export default function CheckoutSuccess({
  onNavigateHome,
  onNavigateCalculator
}: CheckoutSuccessProps) {
  const [orderDetails, setOrderDetails] = useState<OrderDetailsResponse | null>(null);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState<string | null>(null);
  const { clearState } = useCalculatorState();

  // Clear calculator state on successful payment (per user preference)
  useEffect(() => {
    if (orderDetails && (orderDetails.order.status === 'paid' || orderDetails.order.status === 'fulfilled')) {
      clearState();
    }
  }, [orderDetails, clearState]);

  useEffect(() => {
    const fetchOrderDetails = async () => {
      // Get session ID from URL
      const params = new URLSearchParams(window.location.search);
      const sessionId = params.get('session_id');

      if (!sessionId) {
        setError('No session ID found in URL');
        setLoading(false);
        return;
      }

      try {
        const details = await getOrderDetails(sessionId);
        setOrderDetails(details);
      } catch (err) {
        setError('Failed to load order details');
        console.error(err);
      } finally {
        setLoading(false);
      }
    };

    fetchOrderDetails();
  }, []);

  if (loading) {
    return (
      <div className="min-h-screen bg-gradient-to-br from-slate-50 via-blue-50 to-cyan-50 flex items-center justify-center p-6">
        <motion.div
          initial={{ opacity: 0, scale: 0.9 }}
          animate={{ opacity: 1, scale: 1 }}
          className="text-center"
        >
          <div className="animate-spin rounded-full h-16 w-16 border-t-2 border-b-2 border-blue-500 mx-auto"></div>
          <p className="mt-4 text-slate-600 font-medium">Loading your order details...</p>
        </motion.div>
      </div>
    );
  }

  if (error || !orderDetails) {
    return (
      <div className="min-h-screen bg-gradient-to-br from-slate-50 via-blue-50 to-cyan-50 flex items-center justify-center p-6">
        <motion.div
          initial={{ opacity: 0, y: 20 }}
          animate={{ opacity: 1, y: 0 }}
          className="max-w-md w-full bg-white rounded-2xl shadow-luxury p-8 text-center"
        >
          <div className="text-red-500 text-5xl mb-4">⚠️</div>
          <h2 className="text-2xl font-bold text-slate-800 mb-2">Order Not Found</h2>
          <p className="text-slate-600 mb-6">{error || 'Unable to retrieve order details'}</p>
          <button
            onClick={onNavigateHome}
            className="inline-block px-6 py-3 bg-blue-500 text-white rounded-lg hover:bg-blue-600 transition-colors"
          >
            Return to Home
          </button>
        </motion.div>
      </div>
    );
  }

  const { order, session } = orderDetails;
  const totalAmount = order.totalAmount / 100; // Convert cents to dollars
  const baseAmount = order.baseAmount / 100;
  const processingFee = order.processingFee / 100;

  // Use Stripe payment status if available (more accurate for just-completed payments)
  // Otherwise fall back to order status
  const effectiveStatus = session.paymentStatus === 'paid' ? 'paid' : order.status;
  const statusDisplay = getStatusDisplay(effectiveStatus);

  return (
    <>
      {/* Print-specific styles */}
      <style>{`
        @media print {
          body {
            background: white !important;
          }
          .no-print {
            display: none !important;
          }
          .print-receipt {
            max-width: 100% !important;
            margin: 0 !important;
            padding: 20px !important;
            box-shadow: none !important;
          }
          .print-logo {
            max-width: 200px !important;
          }
          @page {
            margin: 0.5in;
          }
        }
      `}</style>

      <div className="min-h-screen bg-gradient-to-br from-slate-50 via-blue-50 to-cyan-50 flex items-center justify-center p-6">
        <motion.div
          initial={{ opacity: 0, y: 20 }}
          animate={{ opacity: 1, y: 0 }}
          transition={{ duration: 0.5 }}
          className="max-w-3xl w-full print-receipt"
        >
          {/* Receipt Container */}
          <div className="bg-white rounded-3xl shadow-2xl overflow-hidden">
            {/* Header with Logo */}
            <motion.div
              initial={{ opacity: 0, y: -20 }}
              animate={{ opacity: 1, y: 0 }}
              transition={{ delay: 0.1 }}
              className="bg-gradient-to-br from-cyan-500 via-blue-500 to-indigo-600 p-8 text-center"
            >
              <img
                src="/puffin-logo.svg"
                alt="Puffin Offset"
                className="h-24 mx-auto mb-4 print-logo"
              />
              <h1 className="text-3xl md:text-4xl font-bold text-white mb-2">
                Order Confirmed
              </h1>
              <p className="text-cyan-50 text-lg">
                Thank you for your carbon offset purchase
              </p>
            </motion.div>

            {/* Success Badge */}
            <motion.div
              initial={{ scale: 0 }}
              animate={{ scale: 1 }}
              transition={{ delay: 0.2, type: 'spring', stiffness: 200 }}
              className="flex justify-center -mt-8 mb-6 no-print"
            >
              <div className="bg-green-500 text-white rounded-full p-6 shadow-xl border-4 border-white">
                <svg className="w-12 h-12" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                  <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={3} d="M5 13l4 4L19 7" />
                </svg>
              </div>
            </motion.div>

            {/* Order Details Section */}
            <motion.div
              initial={{ opacity: 0, y: 20 }}
              animate={{ opacity: 1, y: 0 }}
              transition={{ delay: 0.3 }}
              className="px-8 py-6"
            >
              <h2 className="text-2xl font-bold text-slate-800 mb-6 pb-3 border-b-2 border-slate-200">
                Order Summary
              </h2>

              <div className="space-y-1 mb-6">
                {/* Carbon Offset - Highlighted */}
                <div className="bg-gradient-to-r from-emerald-50 to-teal-50 rounded-xl p-6 mb-4 border-l-4 border-emerald-500">
                  <div className="flex justify-between items-center">
                    <div>
                      <span className="text-sm text-emerald-700 font-medium uppercase tracking-wide">Carbon Offset</span>
                      <p className="text-3xl font-bold text-emerald-900 mt-1">{order.tons} tons CO₂</p>
                    </div>
                    <div className="text-emerald-600">
                      <svg className="w-16 h-16" fill="currentColor" viewBox="0 0 20 20">
                        <path d="M10 2C7.243 2 5 4.243 5 7c0 1.654.843 3.116 2.122 3.975-.076.328-.122.668-.122 1.025v4c0 2.209 1.791 4 4 4s4-1.791 4-4v-4c0-.357-.046-.697-.122-1.025C16.157 10.116 17 8.654 17 7c0-2.757-2.243-5-5-5zm-2 5c0-1.103.897-2 2-2s2 .897 2 2-.897 2-2 2-2-.897-2-2z" />
                      </svg>
                    </div>
                  </div>
                </div>

                {/* Portfolio */}
                <div className="flex justify-between items-center py-4 border-b border-slate-100">
                  <span className="text-slate-600 font-medium">Portfolio</span>
                  <span className="text-slate-800 font-semibold">
                    #{order.portfolioId}
                  </span>
                </div>

                {/* Pricing Breakdown */}
                <div className="bg-slate-50 rounded-lg p-5 my-4 space-y-3">
                  <div className="flex justify-between items-center">
                    <span className="text-slate-700">Offset Cost</span>
                    <span className="text-slate-900 font-semibold">
                      ${formatCurrency(baseAmount)}
                    </span>
                  </div>
                  <div className="flex justify-between items-center">
                    <span className="text-slate-700">Processing Fee (5%)</span>
                    <span className="text-slate-900 font-semibold">
                      ${formatCurrency(processingFee)}
                    </span>
                  </div>
                  <div className="border-t-2 border-slate-300 pt-3 mt-3">
                    <div className="flex justify-between items-center">
                      <span className="text-slate-800 font-bold text-lg">Total Paid</span>
                      <span className="text-blue-600 font-bold text-3xl">
                        ${formatCurrency(totalAmount)}
                      </span>
                    </div>
                  </div>
                </div>
              </div>

              {/* Order Metadata */}
              <div className="bg-gradient-to-r from-slate-50 to-blue-50 rounded-lg p-5 space-y-4">
                <div className="grid grid-cols-1 md:grid-cols-2 gap-5">
                  <div>
                    <span className="text-xs uppercase tracking-wide text-slate-500 font-semibold">Order ID</span>
                    <p className="text-slate-800 font-mono text-sm mt-1 break-all">{order.id}</p>
                  </div>
                  <div>
                    <span className="text-xs uppercase tracking-wide text-slate-500 font-semibold">Status</span>
                    <p className="mt-2">
                      <span className={`inline-block px-4 py-1.5 rounded-full text-sm font-bold ${statusDisplay.className}`}>
                        {statusDisplay.label}
                      </span>
                    </p>
                  </div>
                  {session.customerEmail && (
                    <div className="md:col-span-2">
                      <span className="text-xs uppercase tracking-wide text-slate-500 font-semibold">Email</span>
                      <p className="text-slate-800 font-medium mt-1">{session.customerEmail}</p>
                    </div>
                  )}
                  <div className="md:col-span-2">
                    <span className="text-xs uppercase tracking-wide text-slate-500 font-semibold">Date</span>
                    <p className="text-slate-800 font-medium mt-1">
                      {new Date().toLocaleDateString('en-US', {
                        year: 'numeric',
                        month: 'long',
                        day: 'numeric'
                      })}
                    </p>
                  </div>
                </div>
              </div>
            </motion.div>
          </div>

          {/* Impact Comparisons */}
          <motion.div
            initial={{ opacity: 0, y: 20 }}
            animate={{ opacity: 1, y: 0 }}
            transition={{ delay: 0.4 }}
            className="mt-6"
          >
            <div className="bg-gradient-to-br from-emerald-600 via-teal-600 to-cyan-600 rounded-3xl p-8 shadow-2xl">
              <CarbonImpactComparison tons={order.tons} variant="success" count={3} />
            </div>
          </motion.div>

          {/* Action Buttons */}
          <motion.div
            initial={{ opacity: 0 }}
            animate={{ opacity: 1 }}
            transition={{ delay: 0.5 }}
            className="flex flex-col sm:flex-row gap-4 justify-center mt-8 no-print"
          >
            <button
              onClick={onNavigateHome}
              className="px-8 py-4 bg-gradient-to-r from-blue-500 to-cyan-500 text-white rounded-xl hover:from-blue-600 hover:to-cyan-600 transition-all hover:shadow-xl font-bold text-center transform hover:scale-105"
            >
              Return to Home
            </button>
            <button
              onClick={onNavigateCalculator}
              className="px-8 py-4 bg-gradient-to-r from-green-500 to-emerald-500 text-white rounded-xl hover:from-green-600 hover:to-emerald-600 transition-all hover:shadow-xl font-bold text-center transform hover:scale-105"
            >
              Calculate Another Offset
            </button>
            <button
              onClick={() => window.print()}
              className="px-8 py-4 bg-white text-slate-700 rounded-xl hover:bg-slate-50 transition-all hover:shadow-xl font-bold border-2 border-slate-300 flex items-center justify-center gap-2 transform hover:scale-105"
            >
              <svg className="w-5 h-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M17 17h2a2 2 0 002-2v-4a2 2 0 00-2-2H5a2 2 0 00-2 2v4a2 2 0 002 2h2m2 4h6a2 2 0 002-2v-4a2 2 0 00-2-2H9a2 2 0 00-2 2v4a2 2 0 002 2zm8-12V5a2 2 0 00-2-2H9a2 2 0 00-2 2v4h10z" />
              </svg>
              Print Receipt
            </button>
          </motion.div>

          {/* Confirmation Email Notice */}
          {session.customerEmail && (
            <motion.div
              initial={{ opacity: 0 }}
              animate={{ opacity: 1 }}
              transition={{ delay: 0.6 }}
              className="text-center mt-6 no-print"
            >
              <div className="bg-blue-50 border-l-4 border-blue-500 p-4 rounded-lg inline-block">
                <p className="text-blue-800 font-medium">
                  <svg className="w-5 h-5 inline mr-2" fill="currentColor" viewBox="0 0 20 20">
                    <path d="M2.003 5.884L10 9.882l7.997-3.998A2 2 0 0016 4H4a2 2 0 00-1.997 1.884z" />
                    <path d="M18 8.118l-8 4-8-4V14a2 2 0 002 2h12a2 2 0 002-2V8.118z" />
                  </svg>
                  Confirmation email sent to {session.customerEmail}
                </p>
              </div>
            </motion.div>
          )}

          {/* Footer */}
          <motion.div
            initial={{ opacity: 0 }}
            animate={{ opacity: 1 }}
            transition={{ delay: 0.7 }}
            className="text-center text-slate-500 text-sm mt-8 pb-6 no-print"
          >
            <p>Thank you for making a positive impact on our planet</p>
            <p className="mt-2">Questions? Contact us at support@puffinoffset.com</p>
          </motion.div>
        </motion.div>
      </div>
    </>
  );
}
