import React, { useState } from 'react';
import { Card } from '../ui/card';
import { Button } from '../ui/button';
import { CartItem } from '../commerce/CartItem';
import { PromoCodeModal } from '../commerce/modals/PromoCodeModal';
import { useCart } from '../../context/CartContext';
import {
  ShoppingCart,
  ArrowRight,
  Tag,
  ShieldCheck,
  ChevronLeft,
} from 'lucide-react';

interface CartViewProps {
  onCheckout: () => void;
  onContinueShopping: () => void;
}

export const CartView: React.FC<CartViewProps> = ({
  onCheckout,
  onContinueShopping,
}) => {
  const { cart, removeFromCart, cartTotal } = useCart();
  const [showPromo, setShowPromo] = useState(false);
  const [discount, setDiscount] = useState<{
    code: string;
    amount: number;
  } | null>(null);

  const discountAmount = discount ? cartTotal * (discount.amount / 100) : 0;
  const total = cartTotal - discountAmount;
  const tax = total * 0.08; // Mock tax 8%
  const finalTotal = total + tax;

  const handleApplyPromo = (percent: number, code: string) => {
    setDiscount({ code, amount: percent });
  };

  if (cart.length === 0) {
    return (
      <div className="min-h-[60vh] flex flex-col items-center justify-center animate-fadeIn text-center">
        <div className="w-24 h-24 bg-kodo-ink rounded-full flex items-center justify-center mb-6 border-2 border-dashed border-kodo-steel">
          <ShoppingCart className="w-10 h-10 text-kodo-content-dim" />
        </div>
        <h2 className="text-2xl font-bold text-white mb-2">
          Your cart is empty
        </h2>
        <p className="text-kodo-content-dim mb-8 max-w-sm">
          Looks like you haven't added any sounds yet. Explore the marketplace
          to find your next inspiration.
        </p>
        <Button
          variant="primary"
          onClick={onContinueShopping}
          icon={<ArrowRight className="w-4 h-4" />}
        >
          Browse Marketplace
        </Button>
      </div>
    );
  }

  return (
    <div className="animate-fadeIn max-w-6xl mx-auto pb-20">
      <div className="mb-8">
        <Button
          variant="ghost"
          onClick={onContinueShopping}
          className="mb-4 text-kodo-content-dim hover:text-white pl-0"
        >
          <ChevronLeft className="w-4 h-4 mr-2" /> Continue Shopping
        </Button>
        <h2 className="text-2xl font-display font-bold text-white">
          Your Cart ({cart.length})
        </h2>
      </div>

      <div className="grid grid-cols-1 lg:grid-cols-3 gap-8">
        {/* Cart Items List */}
        <div className="lg:col-span-2 space-y-4">
          {cart.map((item) => (
            <CartItem key={item.cartId} item={item} onRemove={removeFromCart} />
          ))}
        </div>

        {/* Summary Sidebar */}
        <div className="space-y-8">
          <Card variant="gaming" className="p-8">
            <h3 className="font-bold text-white mb-6 uppercase tracking-wider text-sm border-b border-kodo-steel pb-2">
              Order Summary
            </h3>

            <div className="space-y-4 text-sm mb-6">
              <div className="flex justify-between text-kodo-content-dim">
                <span>Subtotal</span>
                <span className="text-white font-mono">
                  ${cartTotal.toFixed(2)}
                </span>
              </div>

              {discount && (
                <div className="flex justify-between text-kodo-lime">
                  <span className="flex items-center gap-2">
                    <Tag className="w-3 h-3" /> Discount ({discount.code})
                  </span>
                  <span className="font-mono">
                    -${discountAmount.toFixed(2)}
                  </span>
                </div>
              )}

              <div className="flex justify-between text-kodo-content-dim">
                <span>Estimated Tax (8%)</span>
                <span className="text-white font-mono">${tax.toFixed(2)}</span>
              </div>
            </div>

            <div className="border-t border-kodo-steel pt-4 mb-6">
              <div className="flex justify-between items-end">
                <span className="font-bold text-white">Total</span>
                <span className="text-2xl font-mono font-bold text-kodo-steel">
                  ${finalTotal.toFixed(2)}
                </span>
              </div>
            </div>

            <Button
              variant="primary"
              className="w-full h-12 text-base shadow-neon-cyan/20"
              onClick={onCheckout}
            >
              PROCEED TO CHECKOUT
            </Button>

            {!discount && (
              <button
                className="w-full text-center text-xs text-kodo-steel mt-4 hover:underline font-bold"
                onClick={() => setShowPromo(true)}
              >
                Have a promo code?
              </button>
            )}
          </Card>

          <div className="bg-kodo-ink/50 p-4 rounded-xl border border-kodo-steel/50 flex items-start gap-4">
            <ShieldCheck className="w-5 h-5 text-kodo-gold flex-shrink-0" />
            <div>
              <h4 className="font-bold text-white text-sm mb-1">
                Secure Checkout
              </h4>
              <p className="text-xs text-kodo-content-dim leading-relaxed">
                All transactions are encrypted and secure. We accept major
                credit cards and crypto.
              </p>
            </div>
          </div>
        </div>
      </div>

      {showPromo && (
        <PromoCodeModal
          onClose={() => setShowPromo(false)}
          onApply={handleApplyPromo}
        />
      )}
    </div>
  );
};
