"use client"

import { useCart } from "@/contexts/cart-context"
import { useAuth } from "@/contexts/auth-context"
import { Button } from "./ui/button"
import { useRouter } from "next/navigation"

export function CartSummary() {
  const { items, getTotalItems, getTotalQuantity } = useCart()
  const { isLoggedIn } = useAuth()
  const router = useRouter()

  const totalPrice = items.reduce((sum, item) => sum + item.price * item.quantity, 0)

  const handleCheckout = () => {
    if (!isLoggedIn) {
      router.push('/login')
    } else {
      router.push('/checkout')
    }
  }

  if (getTotalItems() === 0) {
    return (
      <div className="bg-white p-4 rounded-lg">
        <h2 className="text-lg font-semibold mb-4">Ваша корзина</h2>
        <p className="text-gray-500">В вашей корзине пока нет товаров</p>
      </div>
    )
  }

  return (
    <div className="bg-white p-4 rounded-lg">
      <h2 className="text-lg font-semibold mb-4">Ваша корзина</h2>
      <div className="space-y-2 mb-4">
        <div className="flex justify-between">
          <span>Товары ({getTotalItems()} наименований, {getTotalQuantity()} шт.)</span>
          <span>{totalPrice} ₽</span>
        </div>
      </div>
      <div className="flex justify-between text-lg font-bold mb-4">
        <span>Итого</span>
        <span>{totalPrice} ₽</span>
      </div>
      <Button className="w-full" size="lg" onClick={handleCheckout}>
        {isLoggedIn ? "Перейти к оформлению" : "Войти для оформления"}
      </Button>
    </div>
  )
}

