/**
 * Payment Success Page
 *
 * Displayed after successful Stripe payment. Confirms the payment and activates the booking.
 */

'use client';

import { useState, useEffect, useRef } from 'react';
import { useRouter, useParams, useSearchParams } from 'next/navigation';
import { CheckCircle, Loader2, AlertTriangle, Mail, ArrowRight } from 'lucide-react';
import { confirmBookingPayment } from '@/lib/api/bookings';

export default function PaymentSuccessPage() {
  const router = useRouter();
  const params = useParams();
  const searchParams = useSearchParams();
  const bookingId = params.id as string;
  const sessionId = searchParams.get('session_id');

  const [status, setStatus] = useState<'confirming' | 'success' | 'error'>('confirming');
  const [error, setError] = useState<string | null>(null);
  const confirmedRef = useRef(false);

  useEffect(() => {
    async function confirm() {
      if (!sessionId || !bookingId || confirmedRef.current) return;
      confirmedRef.current = true;

      try {
        await confirmBookingPayment(bookingId, sessionId);
        setStatus('success');
      } catch (err) {
        console.error('Payment confirmation error:', err);
        setError(err instanceof Error ? err.message : 'Erreur lors de la confirmation du paiement');
        setStatus('error');
      }
    }

    confirm();
  }, [bookingId, sessionId]);

  if (!sessionId) {
    return (
      <div className="min-h-screen flex items-center justify-center bg-gradient-to-br from-blue-50 to-indigo-100">
        <div className="bg-white rounded-lg shadow-md p-8 max-w-md text-center">
          <AlertTriangle className="h-12 w-12 text-red-500 mx-auto mb-4" />
          <h2 className="text-xl font-bold text-gray-900 mb-2">Session invalide</h2>
          <p className="text-gray-600 mb-4">Aucune session de paiement trouvee.</p>
          <button
            onClick={() => router.push('/dashboard/bookings')}
            className="px-6 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700"
          >
            Retour aux bookings
          </button>
        </div>
      </div>
    );
  }

  return (
    <div className="min-h-screen flex items-center justify-center bg-gradient-to-br from-blue-50 to-indigo-100 px-4">
      <div className="bg-white rounded-lg shadow-lg p-8 max-w-md w-full text-center">
        {status === 'confirming' && (
          <>
            <Loader2 className="h-16 w-16 animate-spin text-blue-600 mx-auto mb-6" />
            <h2 className="text-xl font-bold text-gray-900 mb-2">Confirmation du paiement...</h2>
            <p className="text-gray-600">
              Veuillez patienter pendant que nous verifions votre paiement et activons votre
              booking.
            </p>
          </>
        )}

        {status === 'success' && (
          <>
            <div className="mb-6">
              <div className="w-20 h-20 bg-green-100 rounded-full flex items-center justify-center mx-auto">
                <CheckCircle className="h-12 w-12 text-green-600" />
              </div>
            </div>
            <h2 className="text-2xl font-bold text-gray-900 mb-3">Paiement confirme !</h2>
            <p className="text-gray-600 mb-6">
              Votre commission a ete payee avec succes. Un email a ete envoye au transporteur avec
              votre demande de booking.
            </p>

            <div className="bg-blue-50 border border-blue-200 rounded-lg p-4 mb-6">
              <div className="flex items-center justify-center space-x-2 text-blue-700">
                <Mail className="h-5 w-5" />
                <span className="text-sm font-medium">Email envoye au transporteur</span>
              </div>
              <p className="text-xs text-blue-600 mt-1">
                Vous recevrez une notification des que le transporteur repond (sous 7 jours max)
              </p>
            </div>

            <button
              onClick={() => router.push('/dashboard/bookings')}
              className="w-full px-6 py-3 bg-blue-600 text-white rounded-lg hover:bg-blue-700 font-semibold flex items-center justify-center"
            >
              Voir mes bookings
              <ArrowRight className="h-4 w-4 ml-2" />
            </button>
          </>
        )}

        {status === 'error' && (
          <>
            <AlertTriangle className="h-16 w-16 text-red-500 mx-auto mb-6" />
            <h2 className="text-xl font-bold text-gray-900 mb-2">Erreur de confirmation</h2>
            <p className="text-gray-600 mb-2">{error}</p>
            <p className="text-sm text-gray-500 mb-6">
              Si votre paiement a ete debite, contactez le support. Votre booking sera active
              manuellement.
            </p>
            <div className="space-y-3">
              <button
                onClick={() => {
                  confirmedRef.current = false;
                  setStatus('confirming');
                  setError(null);
                  confirmBookingPayment(bookingId, sessionId!)
                    .then(() => setStatus('success'))
                    .catch(err => {
                      setError(err instanceof Error ? err.message : 'Erreur');
                      setStatus('error');
                    });
                }}
                className="w-full px-6 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700"
              >
                Reessayer
              </button>
              <button
                onClick={() => router.push('/dashboard/bookings')}
                className="w-full px-6 py-2 bg-gray-200 text-gray-700 rounded-lg hover:bg-gray-300"
              >
                Retour aux bookings
              </button>
            </div>
          </>
        )}
      </div>
    </div>
  );
}
