import { useState, useEffect } from 'react'; import { useSearchParams, useNavigate } from 'react-router-dom'; import { useForm } from 'react-hook-form'; import { zodResolver } from '@hookform/resolvers/zod'; import { z } from 'zod'; import { Layout } from '../components/layout/Layout'; import { Button, Input, Card, CardHeader, CardTitle, CardContent } from '../components/ui'; import { CreditCard, Lock, Check, ArrowLeft, Crown, Zap, Star } from 'lucide-react'; import toast from 'react-hot-toast'; const checkoutSchema = z.object({ cardNumber: z.string().min(16, 'Card number must be 16 digits'), expiryMonth: z.string().min(2, 'Required'), expiryYear: z.string().min(4, 'Required'), cvv: z.string().min(3, 'CVV must be 3 digits'), nameOnCard: z.string().min(1, 'Name on card is required'), billingAddress: z.string().min(1, 'Billing address is required'), city: z.string().min(1, 'City is required'), zipCode: z.string().min(1, 'ZIP code is required'), country: z.string().min(1, 'Country is required'), }); type CheckoutFormData = z.infer; export function CheckoutPage() { const [searchParams] = useSearchParams(); const navigate = useNavigate(); const [isProcessing, setIsProcessing] = useState(false); const [selectedPlan, setSelectedPlan] = useState(searchParams.get('plan') || 'pro'); const { register, handleSubmit, formState: { errors }, watch, } = useForm({ resolver: zodResolver(checkoutSchema), }); const cardNumber = watch('cardNumber'); // Format card number with spaces useEffect(() => { const input = document.querySelector('input[name="cardNumber"]') as HTMLInputElement; if (input && cardNumber) { const formatted = cardNumber.replace(/\s/g, '').replace(/(.{4})/g, '$1 ').trim(); if (formatted !== cardNumber) { input.value = formatted; } } }, [cardNumber]); const plans = { pro: { name: 'Pro', price: 19, icon: Zap, color: 'blue', features: [ 'Unlimited conversations', 'Advanced AI models', 'Priority support', 'API access', 'Custom themes', 'Export conversations', ], }, enterprise: { name: 'Enterprise', price: 99, icon: Crown, color: 'purple', features: [ 'Everything in Pro', 'Team collaboration', 'Admin dashboard', 'SSO integration', 'Custom branding', 'Dedicated support', ], }, }; const currentPlan = plans[selectedPlan as keyof typeof plans]; const PlanIcon = currentPlan.icon; const onSubmit = async (data: CheckoutFormData) => { try { setIsProcessing(true); // Simulate payment processing await new Promise(resolve => setTimeout(resolve, 2000)); // In a real app, you would integrate with Stripe, PayPal, etc. console.log('Processing payment:', { ...data, plan: selectedPlan }); toast.success(`Successfully upgraded to ${currentPlan.name} plan!`); navigate('/profile'); } catch (error: any) { toast.error(error.message || 'Payment processing failed'); } finally { setIsProcessing(false); } }; return (
{/* Header */}

Complete Your Purchase

Upgrade to unlock premium features and unlimited access

{/* Order Summary */}
Order Summary
{/* Plan Selection */}

Select Plan

{Object.entries(plans).map(([key, plan]) => (
setSelectedPlan(key)} >

{plan.name}

{plan.features.length} features included

${plan.price}/month
{selectedPlan === key && (
Selected
)}
))}
{/* Features List */}

What's Included

{currentPlan.features.map((feature, index) => (
{feature}
))}
{/* Pricing Breakdown */}
Subtotal ${currentPlan.price}.00
Tax $0.00
Total ${currentPlan.price}.00/month
{/* Money Back Guarantee */}

30-Day Money Back Guarantee

Cancel anytime within 30 days for a full refund

{/* Payment Form */}
Payment Information
{/* Card Information */}

Card Details

{/* Billing Address */}

Billing Address

{/* Security Notice */}
Your payment information is encrypted and secure
{/* Submit Button */} {/* Terms */}

By completing this purchase, you agree to our{' '} Terms of Service {' '} and{' '} Privacy Policy

); }