import uuid

from sqlalchemy import Column, BigInteger
from sqlalchemy.orm import relationship, backref

from .meta import Base, RBase, UUIDType, foreign_key, now_timestamp


class CartGiftCard(RBase, Base):
    """
    Many to many, Carts to GiftCards.
    A relationship signifies the application of a gift card to a cart.
    """

    id = Column(UUIDType, primary_key=True, index=True)
    cart_id = Column(UUIDType, foreign_key("Cart", "id"), nullable=False)
    gift_card_id = Column(UUIDType, foreign_key("GiftCard", "id"), nullable=False)
    created_timestamp = Column(BigInteger, nullable=False)

    cart = relationship(
        argument="Cart",
        backref=backref("cart_gift_cards", cascade="all, delete-orphan"),
    )

    gift_card = relationship(
        argument="GiftCard",
        backref=backref("gift_card_carts", cascade="all, delete-orphan"),
    )

    def __init__(self, cart=None, gift_card=None):
        self.id = uuid.uuid1()
        self.cart = cart
        self.gift_card = gift_card
        self.created_timestamp = now_timestamp()
