import React, { useState, useEffect, useRef } from 'react';
import { 
  useParams, 
  useNavigate, 
  Link 
} from 'react-router-dom';
import {
  CheckCircle,
  Home,
  ListOrdered,
  Calendar,
  Users,
  CreditCard,
  MapPin,
  Mail,
  Phone,
  User,
  FileText,
  Building2,
  AlertCircle,
  Copy,
  Check,
  Loader2,
} from 'lucide-react';
import { toast } from 'react-toastify';
import {
  getBookingById,
  generateQRCode,
  type Booking,
} from '../../features/bookings/services/bookingService';
import { confirmBankTransfer, type Payment } from '../../features/payments/services/paymentService';
import Loading from '../../shared/components/Loading';
import { useFormatCurrency } from '../../features/payments/hooks/useFormatCurrency';
import { parseDateLocal } from '../../shared/utils/format';
import DepositPaymentModal from '../../features/payments/components/DepositPaymentModal';
import { validateBookingId } from '../../shared/utils/routeValidation';
import { validateAndHandleBookingOwnership } from '../../shared/utils/ownershipValidation';
import { getBookingStatusConfig } from '../../shared/utils/bookingUtils';
import { getPaymentMethodLabel } from '../../shared/utils/paymentUtils';
import { BOOKING_STATUS, PAYMENT_METHOD, PAYMENT_STATUS, PAYMENT_TYPE } from '../../shared/constants/bookingConstants';
import useAuthStore from '../../store/useAuthStore';
import { useCompanySettings } from '../../shared/contexts/CompanySettingsContext';

const BookingSuccessPage: React.FC = () => {
  const { id } = useParams<{ id: string }>();
  const navigate = useNavigate();
  const { formatCurrency } = useFormatCurrency();
  const { userInfo } = useAuthStore();
  const { settings } = useCompanySettings();
  const abortControllerRef = useRef<AbortController | null>(null);

  const [booking, setBooking] = useState<Booking | null>(
    null
  );
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState<string | null>(null);
  const [copiedBookingNumber, setCopiedBookingNumber] = 
    useState(false);
  const [uploadingReceipt, setUploadingReceipt] = 
    useState(false);
  const [receiptUploaded, setReceiptUploaded] = 
    useState(false);
  const [selectedFile, setSelectedFile] = 
    useState<File | null>(null);
  const [showDepositModal, setShowDepositModal] = useState(false);

  // Validate route parameter
  useEffect(() => {
    const validatedId = validateBookingId(id);
    if (!validatedId) {
      setError('Invalid booking ID');
      setLoading(false);
      toast.error('Invalid booking ID');
      navigate('/bookings');
      return;
    }
  }, [id, navigate]);

  // Fetch data with ownership validation and request cancellation
  useEffect(() => {
    const validatedId = validateBookingId(id);
    if (!validatedId) {
      return;
    }

    // Cancel previous request if exists
    if (abortControllerRef.current) {
      abortControllerRef.current.abort();
    }

    // Create new abort controller
    abortControllerRef.current = new AbortController();

    fetchBookingDetails(validatedId);

    // Cleanup: abort request on unmount
    return () => {
      if (abortControllerRef.current) {
        abortControllerRef.current.abort();
      }
    };
  // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [id]);

  /**
   * Fetches booking details with ownership validation
   * Handles automatic redirects for pending Stripe payments
   * Auto-opens deposit modal for bookings requiring deposit
   */
  const fetchBookingDetails = async (bookingId: number) => {
    try {
      setLoading(true);
      setError(null);

      const response = await getBookingById(bookingId);

      if (
        response.success && 
        response.data?.booking
      ) {
        const bookingData = response.data.booking;

        // Validate that the booking belongs to the current user
        if (!validateAndHandleBookingOwnership(bookingData, userInfo?.id, navigate)) {
          setLoading(false);
          return; // Ownership validation handles redirect and error message
        }

        setBooking(bookingData);

        // If Stripe payment is pending, redirect to payment completion page
        if (bookingData.payment_method === PAYMENT_METHOD.STRIPE && bookingData.payments) {
          // Find any pending Stripe payment that needs completion
          const pendingStripePayment = bookingData.payments.find(
            (p: Payment) => 
              p.payment_method === PAYMENT_METHOD.STRIPE && 
              p.payment_status === 'pending' // Payment record status (not booking payment_status)
          );
          
          if (pendingStripePayment) {
            // Redirect to payment page to complete Stripe payment
            navigate(`/payment/${bookingId}`, { replace: true });
            return;
          }
        }

        // Auto-open deposit payment modal for bookings requiring deposit
        // Only show if booking is not cancelled and deposit hasn't been paid
        if (
          bookingData.requires_deposit && 
          !bookingData.deposit_paid &&
          bookingData.status !== BOOKING_STATUS.CANCELLED
        ) {
          setShowDepositModal(true);
        }
      } else {
        throw new Error(
          'Unable to load booking information'
        );
      }
    } catch (err: unknown) {
      // Handle AbortError silently (request was cancelled)
      if (err && typeof err === 'object' && 'name' in err && err.name === 'AbortError') {
        return;
      }
      
      const message =
        (err && typeof err === 'object' && 'response' in err && err.response && typeof err.response === 'object' && 'data' in err.response && err.response.data && typeof err.response.data === 'object' && 'message' in err.response.data && typeof err.response.data.message === 'string' ? err.response.data.message : undefined) ||
        'Unable to load booking information';
      setError(message);
      toast.error(message);
    } finally {
      setLoading(false);
    }
  };

  const formatDate = (dateString: string) => {
    
    const date = parseDateLocal(dateString);
    return date.toLocaleDateString('en-US', {
      weekday: 'long',
      year: 'numeric',
      month: 'long',
      day: 'numeric',
    });
  };

  const formatPrice = (price: number) => formatCurrency(price);

  const copyBookingNumber = async () => {
    if (!booking?.booking_number) return;

    try {
      await navigator.clipboard.writeText(
        booking.booking_number
      );
      setCopiedBookingNumber(true);
      toast.success('Booking number copied');
      setTimeout(() => setCopiedBookingNumber(false), 2000);
    } catch (err) {
      toast.error('Unable to copy');
    }
  };

  /**
   * Handles receipt upload for bank transfer payment confirmation
   * Updates local state optimistically while waiting for backend verification
   */
  const handleUploadReceipt = async () => {
    if (!selectedFile || !booking) return;

    try {
      setUploadingReceipt(true);

      // Generate unique transaction ID for tracking
      const transactionId = 
        `TXN-${booking.booking_number}-${Date.now()}`;

      const response = await confirmBankTransfer(
        booking.id,
        transactionId,
        selectedFile
      );

      if (response.success) {
        toast.success(
          '✅ Payment confirmation sent successfully! ' +
          'We will confirm as soon as possible.'
        );
        setReceiptUploaded(true);
        
        // Optimistically update booking state
        // Backend will verify and update actual status
        setBooking((prev) =>
          prev
            ? { 
                ...prev, 
                payment_status: PAYMENT_STATUS.PAID,
                // Auto-confirm pending bookings after payment confirmation
                status: prev.status === BOOKING_STATUS.PENDING 
                  ? BOOKING_STATUS.CONFIRMED 
                  : prev.status 
              }
            : null
        );
      } else {
        throw new Error(
          response.message || 
          'Unable to confirm payment'
        );
      }
    } catch (err: unknown) {
      const message =
        (err && typeof err === 'object' && 'response' in err && err.response && typeof err.response === 'object' && 'data' in err.response && err.response.data && typeof err.response.data === 'object' && 'message' in err.response.data && typeof err.response.data.message === 'string' ? err.response.data.message : undefined) ||
        'Unable to send payment confirmation. ' +
        'Please try again.';
      toast.error(message);
    } finally {
      setUploadingReceipt(false);
    }
  };

  const qrCodeUrl = booking
    ? generateQRCode(
        booking.booking_number,
        booking.total_price,
        settings.bank_code || undefined,
        settings.bank_account_number || undefined,
        settings.bank_account_holder || undefined
      )
    : null;

  if (loading) {
    return <Loading fullScreen text="Loading..." />;
  }

  if (error || !booking) {
    return (
      <div className="min-h-screen bg-gray-50 py-8">
        <div className="max-w-4xl mx-auto px-4">
          <div
            className="bg-red-50 border border-red-200 
              rounded-lg p-8 text-center"
          >
            <AlertCircle
              className="w-12 h-12 text-red-500 
                mx-auto mb-3"
            />
            <p className="text-red-700 font-medium mb-4">
              {error || 'Booking not found'}
            </p>
            <button
              onClick={() => navigate('/rooms')}
              className="inline-flex items-center gap-2 bg-indigo-600 
            text-white px-3 py-2 rounded-md hover:bg-indigo-700 
            disabled:bg-gray-400 mb-6 transition-colors"
            >
              Back to room list
            </button>
          </div>
        </div>
      </div>
    );
  }

  const room = booking.room;
  const roomType = room?.room_type;

  /**
   * Determines if payment is completed by checking multiple payment indicators
   * Handles both full payment and deposit-only bookings
   */
  const isPaymentCompleted = (() => {
    // Cancelled bookings are never considered paid
    if (booking.status === BOOKING_STATUS.CANCELLED) {
      return false;
    }
    
    // Check booking-level payment status
    if (booking.payment_status === PAYMENT_STATUS.PAID) {
      return true;
    }
    
    // Check payment balance indicator
    if (booking.payment_balance?.is_fully_paid === true) {
      return true;
    }
    
    // For deposit bookings, check if deposit flag is set
    if (booking.requires_deposit && booking.deposit_paid === true) {
      return true;
    }
    
    // Check individual payment records for more granular status
    if (booking.payments && Array.isArray(booking.payments)) {
      // Calculate total from completed payment records
      const totalPaid = booking.payments
        .filter((p: Payment) => p.payment_status === 'completed') // Payment record status
        .reduce((sum: number, p: Payment) => sum + parseFloat(p.amount?.toString() || '0'), 0);
      
      // For deposit bookings, check if deposit payment is completed
      if (booking.requires_deposit) {
        const depositPayment = booking.payments.find(
          (p: Payment) => p.payment_type === PAYMENT_TYPE.DEPOSIT && p.payment_status === 'completed'
        );
        if (depositPayment) {
          return true;
        }
      } else {
        // For full payment bookings, check if total paid meets or exceeds booking price
        // Allow small floating point differences (0.01) for currency calculations
        return totalPaid >= booking.total_price - 0.01;
      }
    }
    
    return false;
  })();

  const isCancelled = booking.status === BOOKING_STATUS.CANCELLED;
  const statusConfig = getBookingStatusConfig(booking.status);

  return (
    <div className="min-h-screen bg-gray-50 py-8">
      <div className="max-w-4xl mx-auto px-4">
        {}
        <div
          className="bg-white rounded-lg shadow-md 
            p-8 mb-6 text-center"
        >
          {isCancelled ? (
            <>
              <div
                className="w-20 h-20 bg-red-100 
                  rounded-full flex items-center 
                  justify-center mx-auto mb-4"
              >
                <AlertCircle 
                  className="w-12 h-12 text-red-600" 
                />
              </div>
              <h1
                className="text-3xl font-bold text-gray-900 
                  mb-2"
              >
                Booking Cancelled
              </h1>
              <p className="text-gray-600 mb-4">
                This booking has been cancelled
              </p>
            </>
          ) : isPaymentCompleted ? (
            <>
              <div
                className="w-20 h-20 bg-green-100 
                  rounded-full flex items-center 
                  justify-center mx-auto mb-4"
              >
                <CheckCircle 
                  className="w-12 h-12 text-green-600" 
                />
              </div>
              <h1
                className="text-3xl font-bold text-gray-900 
                  mb-2"
              >
                Booking Successful!
              </h1>
              <p className="text-gray-600 mb-4">
                Thank you for booking with our hotel
              </p>
            </>
          ) : (
            <>
              <div
                className="w-20 h-20 bg-yellow-100 
                  rounded-full flex items-center 
                  justify-center mx-auto mb-4"
              >
                <AlertCircle 
                  className="w-12 h-12 text-yellow-600" 
                />
              </div>
              <h1
                className="text-3xl font-bold text-gray-900 
                  mb-2"
              >
                Booking Pending Payment
              </h1>
              <p className="text-gray-600 mb-4">
                Please complete your payment to confirm your booking
              </p>
            </>
          )}

          {}
          <div
            className="inline-flex items-center gap-2 
              bg-indigo-50 px-6 py-3 rounded-lg"
          >
            <span className="text-sm text-indigo-600 
              font-medium"
            >
              Booking Number:
            </span>
            <span className="text-lg font-bold 
              text-indigo-900"
            >
              {booking.booking_number}
            </span>
            <button
              onClick={copyBookingNumber}
              className="ml-2 p-1 hover:bg-indigo-100 
                rounded transition-colors"
              title="Copy booking number"
            >
              {copiedBookingNumber ? (
                <Check className="w-4 h-4 text-green-600" />
              ) : (
                <Copy className="w-4 h-4 text-indigo-600" />
              )}
            </button>
          </div>

          {}
          <div className="mt-4">
            <span
              className={`inline-flex items-center gap-2 px-4 py-2 
                rounded-full text-sm font-medium 
                ${statusConfig.color}`}
            >
              <statusConfig.icon className="w-4 h-4" />
              {statusConfig.text}
            </span>
          </div>
        </div>

        {}
        <div className="bg-white rounded-lg shadow-md 
          p-6 mb-6"
        >
          <h2 className="text-xl font-bold text-gray-900 
            mb-4"
          >
            Booking Details
          </h2>

          <div className="space-y-4">
            {}
            {roomType && (
              <div className="border-b pb-4">
                <div className="flex items-start gap-4">
                  {((room?.room_type?.images && room.room_type.images.length > 0) 
                    ? room.room_type.images[0] 
                    : roomType?.images?.[0]) && (
                    <img
                      src={(room?.room_type?.images && room.room_type.images.length > 0) 
                        ? room.room_type.images[0] 
                        : (roomType?.images?.[0] || '')}
                      alt={roomType?.name || 'Room'}
                      className="w-24 h-24 object-cover 
                        rounded-lg"
                    />
                  )}
                  <div className="flex-1">
                    <h3 className="font-bold text-lg 
                      text-gray-900"
                    >
                      {roomType.name}
                    </h3>
                    {room && (
                      <p className="text-gray-600 text-sm">
                        <MapPin className="w-4 h-4 
                          inline mr-1" 
                        />
                        Room {room.room_number} - 
                        Floor {room.floor}
                      </p>
                    )}
                    <p className="text-indigo-600 
                      font-semibold mt-1"
                    >
                      {formatPrice(room?.room_type?.base_price || roomType?.base_price || 0)}/night
                    </p>
                  </div>
                </div>
              </div>
            )}

            {}
            <div className="grid grid-cols-1 md:grid-cols-2 
              gap-4"
            >
              <div>
                <p className="text-sm text-gray-600 mb-1">
                  <Calendar className="w-4 h-4 inline mr-1" />
                  Check-in Date
                </p>
                <p className="font-medium text-gray-900">
                  {formatDate(booking.check_in_date)}
                </p>
              </div>
              <div>
                <p className="text-sm text-gray-600 mb-1">
                  <Calendar className="w-4 h-4 inline mr-1" />
                  Check-out Date
                </p>
                <p className="font-medium text-gray-900">
                  {formatDate(booking.check_out_date)}
                </p>
              </div>
            </div>

            {}
            <div>
              <p className="text-sm text-gray-600 mb-1">
                <Users className="w-4 h-4 inline mr-1" />
                Number of Guests
              </p>
              <p className="font-medium text-gray-900">
                {booking.guest_count} guest(s)
              </p>
            </div>

            {}
            {booking.notes && (
              <div>
                <p className="text-sm text-gray-600 mb-1">
                  <FileText className="w-4 h-4 inline mr-1" />
                  Notes
                </p>
                <p className="font-medium text-gray-900">
                  {booking.notes}
                </p>
              </div>
            )}

            {}
            <div className="border-t pt-4">
              <p className="text-sm text-gray-600 mb-1">
                <CreditCard className="w-4 h-4 inline mr-1" />
                Payment Method
              </p>
              <p className="font-medium text-gray-900">
                {booking.payment_method === PAYMENT_METHOD.CASH
                  ? '💵 Pay at hotel'
                  : `🏦 ${getPaymentMethodLabel(booking.payment_method)}`}
              </p>
            </div>

            {}
            <div className="border-t pt-4">
              {booking.original_price && booking.discount_amount && booking.discount_amount > 0 ? (
                <>
                  <div className="mb-2">
                    <div className="flex justify-between items-center mb-1">
                      <span className="text-sm text-gray-600">Subtotal:</span>
                      <span className="text-base font-semibold text-gray-900">{formatPrice(booking.original_price)}</span>
                    </div>
                    <div className="flex justify-between items-center mb-2">
                      <span className="text-sm text-green-600">
                        Discount{booking.promotion_code ? ` (${booking.promotion_code})` : ''}:
                      </span>
                      <span className="text-base font-semibold text-green-600">-{formatPrice(booking.discount_amount)}</span>
                    </div>
                    <div className="border-t border-gray-300 pt-2 mt-2">
                      <div className="flex justify-between items-center">
                        <span className="text-lg font-semibold text-gray-900">Total Payment</span>
                        <span className="text-2xl font-bold text-indigo-600">{formatPrice(booking.total_price)}</span>
                      </div>
                    </div>
                  </div>
                </>
              ) : (
                <div className="flex justify-between items-center">
                  <span className="text-lg font-semibold text-gray-900">Total Payment</span>
                  <span className="text-2xl font-bold text-indigo-600">{formatPrice(booking.total_price)}</span>
                </div>
              )}
            </div>
          </div>
        </div>

        {}
        {booking.guest_info && (
          <div className="bg-white rounded-lg shadow-md 
            p-6 mb-6"
          >
            <h2 className="text-xl font-bold text-gray-900 
              mb-4"
            >
              Customer Information
            </h2>
            <div className="space-y-3">
              <div>
                <p className="text-sm text-gray-600">
                  <User className="w-4 h-4 inline mr-1" />
                  Full Name
                </p>
                <p className="font-medium text-gray-900">
                  {booking.guest_info.full_name}
                </p>
              </div>
              <div>
                <p className="text-sm text-gray-600">
                  <Mail className="w-4 h-4 inline mr-1" />
                  Email
                </p>
                <p className="font-medium text-gray-900">
                  {booking.guest_info.email}
                </p>
              </div>
              <div>
                <p className="text-sm text-gray-600">
                  <Phone className="w-4 h-4 inline mr-1" />
                  Phone Number
                </p>
                <p className="font-medium text-gray-900">
                  {booking.guest_info.phone}
                </p>
              </div>
            </div>
          </div>
        )}

        {}
        {(booking.payment_method === PAYMENT_METHOD.CASH || booking.payment_method === PAYMENT_METHOD.BANK_TRANSFER) && (
          <div
            className="bg-blue-50 border border-blue-200 
              rounded-lg p-6 mb-6"
          >
            <div className="flex items-start gap-3 mb-4">
              <Building2 
                className="w-6 h-6 text-blue-600 
                  mt-1 flex-shrink-0" 
              />
              <div className="flex-1">
                <h3 className="font-bold text-blue-900 mb-2">
                  Bank Transfer Instructions
                </h3>
                <div className="space-y-2 text-sm 
                  text-blue-800"
                >
                  <p>
                    Please transfer according to the following information:
                  </p>
                  
                  <div className="grid grid-cols-1 
                    md:grid-cols-2 gap-4"
                  >
                    {}
                    <div className="bg-white rounded-lg 
                      p-4 space-y-2"
                    >
                      <p>
                        <strong>Bank:</strong> 
                        {settings.bank_name || 'Not configured'}
                      </p>
                      <p>
                        <strong>Account Number:</strong> 
                        {settings.bank_account_number || 'Not configured'}
                      </p>
                      <p>
                        <strong>Account Holder:</strong> 
                        {settings.bank_account_holder || 'Not configured'}
                      </p>
                      <p>
                        <strong>Amount:</strong>{' '}
                        <span className="text-indigo-600 
                          font-bold"
                        >
                          {formatPrice(booking.total_price)}
                        </span>
                      </p>
                      <p>
                        <strong>Content:</strong>{' '}
                        <span className="font-mono 
                          text-indigo-600"
                        >
                          {booking.booking_number}
                        </span>
                      </p>
                    </div>

                    {}
                    {qrCodeUrl && (
                      <div className="bg-white rounded-lg 
                        p-4 flex flex-col items-center 
                        justify-center"
                      >
                        <p className="text-sm font-medium 
                          text-gray-700 mb-2"
                        >
                          Scan QR code to transfer
                        </p>
                        <img
                          src={qrCodeUrl}
                          alt="QR Code"
                          className="w-48 h-48 border-2 
                            border-gray-200 rounded-lg"
                        />
                        <p className="text-xs text-gray-500 
                          mt-2 text-center"
                        >
                          QR code includes all information
                        </p>
                      </div>
                    )}
                  </div>

                  <p className="text-xs italic mt-2">
                    💡 Note: Please enter the correct booking number 
                    in the transfer content so we can confirm your payment.
                  </p>
                </div>
              </div>
            </div>

            {}
            {!receiptUploaded ? (
              <div className="border-t border-blue-200 
                pt-4"
              >
                <h4 className="font-semibold text-blue-900 
                  mb-3"
                >
                  📎 Payment Confirmation
                </h4>
                <p className="text-sm text-blue-700 mb-3">
                  After transferring, please upload 
                  the receipt image so we can confirm faster.
                </p>

                <div className="space-y-3">
                  {}
                  <div>
                    <label
                      htmlFor="receipt-upload"
                      className="block w-full px-4 py-3 
                        border-2 border-dashed 
                        border-blue-300 rounded-lg 
                        text-center cursor-pointer 
                        hover:border-blue-400 
                        hover:bg-blue-100/50 
                        transition-colors"
                    >
                      <input
                        id="receipt-upload"
                        type="file"
                        accept="image/*"
                        onChange={(e) => {
                          const file = e.target.files?.[0];
                          if (file) {
                            setSelectedFile(file);
                          }
                        }}
                        className="hidden"
                      />
                      <span className="text-sm text-gray-600">
                        Click to upload receipt
                      </span>
                    </label>
                  </div>
                  {selectedFile && (
                    <button
                      onClick={handleUploadReceipt}
                      disabled={uploadingReceipt}
                      className="w-full px-4 py-3 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition-colors font-semibold disabled:bg-gray-400 disabled:cursor-not-allowed flex items-center justify-center gap-2"
                    >
                      {uploadingReceipt ? (
                        <>
                          <Loader2 
                            className="w-5 h-5 
                              animate-spin" 
                          />
                          Sending...
                        </>
                      ) : (
                        <>
                          <CheckCircle 
                            className="w-5 h-5" 
                          />
                          Confirm payment completed
                        </>
                      )}
                    </button>
                  )}
                </div>
              </div>
            ) : (
              <div className="border-t border-green-200 
                pt-4 bg-green-50 rounded-lg p-4"
              >
                <div className="flex items-center 
                  gap-3"
                >
                  <CheckCircle 
                    className="w-6 h-6 text-green-600 
                      flex-shrink-0" 
                  />
                  <div>
                    <p className="font-semibold 
                      text-green-900"
                    >
                      Payment confirmation sent
                    </p>
                    <p className="text-sm text-green-700">
                      We will confirm your order 
                      as soon as possible.
                    </p>
                  </div>
                </div>
              </div>
            )}
          </div>
        )}

        {}
        <div
          className="bg-yellow-50 border border-yellow-200 
            rounded-lg p-4 mb-6"
        >
          <p className="text-sm text-yellow-800">
            ⚠️ <strong>Important Notice:</strong>
          </p>
          <ul className="text-sm text-yellow-700 mt-2 
            space-y-1 ml-4 list-disc"
          >
            <li>
              Please bring your ID card when checking in
            </li>
            <li>
              Check-in time: 14:00 / 
              Check-out time: 12:00
            </li>
            <li>
              If you cancel the booking, 20% of 
              the total order value will be charged
            </li>
            {(booking.payment_method === PAYMENT_METHOD.CASH || booking.payment_method === PAYMENT_METHOD.BANK_TRANSFER) && (
              <li>
                Please transfer within 24 hours 
                to secure your room
              </li>
            )}
          </ul>
        </div>

        {}
        <div className="flex flex-col sm:flex-row gap-4">
          <Link
            to="/bookings"
            className="flex-1 flex items-center 
              justify-center gap-2 px-6 py-3 
              bg-indigo-600 text-white rounded-lg 
              hover:bg-indigo-700 transition-colors 
              font-semibold"
          >
            <ListOrdered className="w-5 h-5" />
            View My Bookings
          </Link>
          <Link
            to="/"
            className="flex-1 flex items-center 
              justify-center gap-2 px-6 py-3 
              bg-gray-600 text-white rounded-lg 
              hover:bg-gray-700 transition-colors 
              font-semibold"
          >
            <Home className="w-5 h-5" />
            Go to Home
          </Link>
        </div>
      </div>

      {/* Deposit Payment Modal */}
      {showDepositModal && id && (
        <DepositPaymentModal
          isOpen={showDepositModal}
          bookingId={Number(id)}
          onClose={() => setShowDepositModal(false)}
          onSuccess={() => {
            setShowDepositModal(false);
            if (id) {
              fetchBookingDetails(Number(id));
            }
          }}
        />
      )}
    </div>
  );
};

export default BookingSuccessPage;
