"""
byceps.services.ticketing.ticket_bundle_service
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:Copyright: 2006-2017 Jochen Kupperschmidt
:License: Modified BSD, see LICENSE for details.
"""

from typing import Optional, Sequence

from ...database import db
from ...typing import UserID

from ..seating.models.category import CategoryID

from .models.ticket import Ticket
from .models.ticket_bundle import TicketBundle, TicketBundleID
from .ticket_service import build_tickets


def create_bundle(category_id: CategoryID, ticket_quantity: int,
                  owned_by_id: UserID) -> TicketBundle:
    """Create a ticket bundle and the given quantity of tickets."""
    if ticket_quantity < 1:
        raise ValueError('Ticket quantity must be positive.')

    bundle = TicketBundle(category_id, ticket_quantity, owned_by_id)
    db.session.add(bundle)

    tickets = list(build_tickets(category_id, owned_by_id, ticket_quantity,
                                 bundle=bundle))
    db.session.add_all(tickets)

    db.session.commit()

    return bundle


def delete_bundle(bundle: TicketBundle) -> None:
    """Delete the ticket bundle and the tickets associated with it."""
    for ticket in bundle.tickets:
        db.session.delete(ticket)

    db.session.delete(bundle)

    db.session.commit()


def find_bundle(bundle_id: TicketBundleID) -> Optional[TicketBundle]:
    """Return the ticket bundle with that id, or `None` if not found."""
    return TicketBundle.query.get(bundle_id)


def find_tickets_for_bundle(bundle_id: TicketBundleID) -> Sequence[Ticket]:
    """Return all tickets included in this bundle."""
    return Ticket.query \
        .filter(Ticket.bundle_id == bundle_id) \
        .all()

@@ -1062,21 +1062,21 @@
 e%0A%0A%0Adef 
-delet
+revok
 e_bundle
@@ -1118,13 +1118,13 @@
  %22%22%22
-Delet
+Revok
 e th
@@ -1135,50 +1135,33 @@
 cket
- bundle and the tickets associated with it
+s included in this bundle
 .%22%22%22
@@ -1207,64 +1207,29 @@
     
-db.session.delete(ticket)%0A%0A    db.session.delete(bundle)
+ticket.revoked = True
 %0A%0A  

Revoke the tickets in a bundle instead of deleting the bundle and its tickets