import React, { useState, useEffect } from 'react';
import { createPayPalOrder } from '../services/paymentService';
import { X, Loader2, AlertCircle } from 'lucide-react';
import { toast } from 'react-toastify';
import { useFormatCurrency } from '../hooks/useFormatCurrency';

interface PayPalPaymentModalProps {
  isOpen: boolean;
  bookingId: number;
  amount: number;
  currency?: string;
  onSuccess: () => void;
  onClose: () => void;
}

const PayPalPaymentModal: React.FC<PayPalPaymentModalProps> = ({
  isOpen,
  bookingId,
  amount,
  currency: propCurrency,
  onSuccess: _onSuccess,
  onClose,
}) => {
  const { currency: contextCurrency } = useFormatCurrency();
  const currency = propCurrency || contextCurrency || 'USD';
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState<string | null>(null);
  const [approvalUrl, setApprovalUrl] = useState<string | null>(null);

  useEffect(() => {
    if (!isOpen) return;

    const initializePayPal = async () => {
      try {
        setLoading(true);
        setError(null);

        const currentUrl = window.location.origin;
        const returnUrl = `${currentUrl}/payment/paypal/return?bookingId=${bookingId}`;
        const cancelUrl = `${currentUrl}/payment/paypal/cancel?bookingId=${bookingId}`;

        const response = await createPayPalOrder(
          bookingId,
          amount,
          currency,
          returnUrl,
          cancelUrl
        );

        if (response.success && response.data) {
          const { approval_url } = response.data;
          
          if (!approval_url) {
            throw new Error('Approval URL not received from server');
          }
          
          setApprovalUrl(approval_url);
        } else {
          throw new Error(response.message || 'Failed to initialize PayPal payment');
        }
      } catch (err: any) {
        console.error('Error initializing PayPal:', err);
        const errorMessage = err.response?.data?.message || err.message || 'Failed to initialize PayPal payment';
        setError(errorMessage);
        toast.error(errorMessage);
      } finally {
        setLoading(false);
      }
    };

    initializePayPal();
  }, [isOpen, bookingId, amount, currency]);

  const handlePayPalClick = () => {
    if (approvalUrl) {
      window.location.href = approvalUrl;
    }
  };

  if (!isOpen) return null;

  return (
    <div className="fixed inset-0 z-[10000] flex items-center justify-center p-2 sm:p-4">
      <div
        className="fixed inset-0 bg-black/90 backdrop-blur-sm"
        onClick={onClose}
      />
      <div className="relative w-full max-w-md max-h-[95vh] bg-gradient-to-br from-[#0a0a0a] via-[#1a1a1a] to-[#0a0a0a] rounded-xl sm:rounded-2xl border border-[#d4af37]/30 shadow-2xl shadow-[#d4af37]/20 overflow-hidden flex flex-col">
        {/* Header */}
        <div className="px-3 sm:px-4 md:px-6 py-3 sm:py-4 border-b border-[#d4af37]/20 bg-gradient-to-r from-[#1a1a1a] to-[#0a0a0a] flex-shrink-0">
          <div className="flex items-center justify-between gap-2">
            <h2 className="text-base sm:text-lg md:text-xl font-serif font-bold text-white tracking-tight truncate flex-1">
              PayPal Payment
            </h2>
            <button
              onClick={onClose}
              className="p-1.5 sm:p-2 hover:bg-[#d4af37]/10 rounded-lg transition-colors text-gray-400 hover:text-white flex-shrink-0"
            >
              <X className="w-4 h-4 sm:w-5 sm:h-5" />
            </button>
          </div>
        </div>

        {/* Content */}
        <div className="flex-1 overflow-y-auto p-3 sm:p-4 md:p-6">
          {loading ? (
            <div className="flex flex-col items-center justify-center py-8 sm:py-12">
              <Loader2 className="w-10 h-10 sm:w-12 sm:h-12 animate-spin text-[#d4af37] mb-3 sm:mb-4" />
              <p className="text-xs sm:text-sm text-gray-400">Initializing PayPal payment...</p>
            </div>
          ) : error ? (
            <div className="bg-red-500/10 border border-red-500/30 rounded-lg p-3 sm:p-4">
              <div className="flex items-start gap-2 sm:gap-3">
                <AlertCircle className="w-4 h-4 sm:w-5 sm:h-5 text-red-400 mt-0.5 flex-shrink-0" />
                <div className="min-w-0 flex-1">
                  <h3 className="text-xs sm:text-sm font-semibold text-red-300 mb-1">
                    Payment Initialization Failed
                  </h3>
                  <p className="text-xs text-red-200/80 leading-relaxed">
                    {error || 'Unable to initialize PayPal payment. Please try again.'}
                  </p>
                </div>
              </div>
            </div>
          ) : approvalUrl ? (
            <div className="space-y-4 sm:space-y-6">
              <div className="text-center">
                <div className="mb-3 sm:mb-4 flex justify-center">
                  <svg
                    className="h-10 sm:h-12 w-auto"
                    viewBox="0 0 283 64"
                    fill="none"
                    xmlns="http://www.w3.org/2000/svg"
                  >
                    <path
                      d="M141.04 16c-11.04 0-19 7.2-19 18s8.96 18 20 18c6.67 0 12.55-2.64 16.19-7.09l-7.65-4.42c-2.02 2.21-5.09 3.5-8.54 3.5-4.79 0-8.86-2.5-10.37-6.5h28.02c.22-1.12.35-2.28.35-3.5 0-10.79-7.96-17.99-19-17.99zm-9.46 14.5c1.25-3.99 4.67-6.5 9.45-6.5 4.79 0 8.21 2.51 9.45 6.5h-18.9zM248.72 16c-11.04 0-19 7.2-19 18s8.96 18 20 18c6.67 0 12.55-2.64 16.19-7.09l-7.65-4.42c-2.02 2.21-5.09 3.5-8.54 3.5-4.79 0-8.86-2.5-10.37-6.5h28.02c.22-1.12.35-2.28.35-3.5 0-10.79-7.96-17.99-19-17.99zm-9.45 14.5c1.25-3.99 4.67-6.5 9.45-6.5 4.79 0 8.21 2.51 9.45 6.5h-18.9zM200.24 34c0 6 3.92 10 10 10 4.12 0 7.21-1.87 8.8-4.92l7.68 4.43c-3.18 5.3-9.14 8.49-16.48 8.49-11.05 0-19-7.2-19-18s7.96-18 19-18c7.34 0 13.29 3.19 16.48 8.49l-7.68 4.43c-1.59-3.05-4.68-4.92-8.8-4.92-6.07 0-10 4-10 10zm82.48-29v46h-9V5h9zM36.95 0L73.9 64H0L36.95 0zm92.38 5l-27.71 48L73.91 5H84.3l17.32 30 17.32-30h10.39zm58.91 12v9.69c-1-.29-2.06-.49-3.2-.49-5.81 0-10 4-10 10V51h-9V17h9v9.2c0-5.08 5.7-9.2 12.2-9.2z"
                      fill="#003087"
                    />
                  </svg>
                </div>
                <h3 className="text-base sm:text-lg font-serif font-semibold text-white mb-1.5 sm:mb-2">
                  Complete Payment with PayPal
                </h3>
                <p className="text-xs sm:text-sm text-gray-400 mb-3 sm:mb-4 leading-relaxed px-2">
                  You will be redirected to PayPal to securely complete your payment of{' '}
                  <span className="font-semibold text-[#d4af37]">
                    {new Intl.NumberFormat('en-US', {
                      style: 'currency',
                      currency: currency,
                    }).format(amount)}
                  </span>
                </p>
              </div>
              <button
                onClick={handlePayPalClick}
                className="w-full bg-gradient-to-r from-[#0070ba] to-[#005ea6] 
                  hover:from-[#0080cc] hover:to-[#0070ba] text-white 
                  font-semibold py-2.5 sm:py-3 px-4 sm:px-6 rounded-lg transition-all duration-300 
                  flex items-center justify-center gap-2 sm:gap-3 shadow-lg shadow-blue-500/30
                  hover:shadow-xl hover:shadow-blue-500/40 text-sm sm:text-base min-h-[44px]"
              >
                <svg
                  className="w-4 h-4 sm:w-5 sm:h-5 flex-shrink-0"
                  viewBox="0 0 24 24"
                  fill="currentColor"
                >
                  <path d="M7.076 21.337H2.47a.641.641 0 0 1-.633-.74L4.944.901C5.026.382 5.474 0 5.998 0h7.46c2.57 0 4.578.543 5.69 1.81 1.01 1.15 1.304 2.42 1.012 4.287-.023.143-.047.288-.077.437-.983 5.05-4.349 6.797-8.647 6.797h-2.19c-.524 0-.968.382-1.05.9l-1.12 7.203zm14.146-14.42a.477.477 0 0 0-.414-.24h-3.84c-.48 0-.856.355-.932.826-.075.47-.232 1.21-.232 1.21s-.156-.74-.232-1.21a.957.957 0 0 0-.932-.826H5.342a.957.957 0 0 0-.932.826c-.076.47-.232 1.21-.232 1.21s-.156-.74-.232-1.21a.957.957 0 0 0-.932-.826H.477a.477.477 0 0 0-.414.24c-.11.19-.14.426-.08.643.06.217.2.4.388.51l.04.02c.19.11.426.14.643.08.217-.06.4-.2.51-.388l.01-.02c.11-.19.14-.426.08-.643a.955.955 0 0 0-.388-.51l-.01-.01a.955.955 0 0 0-.51-.388.955.955 0 0 0-.643.08l-.01.01a.955.955 0 0 0-.388.51c-.06.217-.03.453.08.643l.01.02c.11.188.293.328.51.388.217.06.453.03.643-.08l.01-.02c.188-.11.328-.293.388-.51.06-.217.03-.453-.08-.643l-.01-.01z"/>
                </svg>
                <span>Pay with PayPal</span>
              </button>
              <p className="text-xs text-gray-500 text-center">
                Secure payment powered by PayPal
              </p>
            </div>
          ) : (
            <div className="flex flex-col items-center justify-center py-8 sm:py-12">
              <Loader2 className="w-10 h-10 sm:w-12 sm:h-12 animate-spin text-[#d4af37] mb-3 sm:mb-4" />
              <p className="text-xs sm:text-sm text-gray-400">Loading PayPal...</p>
            </div>
          )}
        </div>
      </div>
    </div>
  );
};

export default PayPalPaymentModal;

