from django.db import models

# In a suitable models.py file, e.g., api/producers_api/orders/models.py or a new 'cart' app's models.py

from django.db import models
from django.contrib.auth import get_user_model

from common.models.base import TimeStampedModel
from producers_products_variants.models.products_vairants import ProductsVariants

# from api.producers_api.accounts.models import User
from users.models import User


class Cart(TimeStampedModel):
    """
    Represents a user's shopping cart.
    """
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='cart', null=True, blank=True)
    # total_items = models.PositiveIntegerField(default=0) # To store the count of items in the cart

    def __str__(self):
        return f"Cart of {self.user.username if self.user else 'Anonymous User'}"

class CartItem(TimeStampedModel):
    """
    Represents an individual item within a shopping cart.
    """
    cart = models.ForeignKey(Cart, on_delete=models.CASCADE, related_name='items')
    product_variant = models.ForeignKey(ProductsVariants, on_delete=models.CASCADE)
    quantity = models.PositiveIntegerField(default=1)

    def __str__(self):
        return f"{self.quantity} x {self.product_variant.product.name} in {self.cart}"

    @property
    def total_price(self):
        """Calculates the total price for this cart item."""
        return self.quantity * self.product_variant.product.price

