# -*- coding: utf-8 -*-

"""
byceps.services.user_badge.service
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

from collections import defaultdict

from ...database import db

from .models import Badge, BadgeAwarding


def create_badge(label, image_filename, *, brand_id=None, description=None):
    """Introduce a new badge."""
    badge = Badge(label, image_filename, brand_id=brand_id,
                  description=description)

    db.session.add(badge)
    db.session.commit()

    return badge.to_tuple()


def find_badge(badge_id):
    """Return the badge with that id, or `None` if not found."""
    badge = Badge.query.get(badge_id)

    return badge.to_tuple()


def get_awardings_of_badge(badge_id):
    """Return the awardings (user and date) of this badge."""
    awardings = BadgeAwarding.query \
        .filter_by(badge_id=badge_id) \
        .all()

    return [awarding.to_tuple() for awarding in awardings]


def get_badges_for_user(user_id):
    """Return all badges that have been awarded to the user."""
    badges = Badge.query \
        .join(BadgeAwarding).filter_by(user_id=user_id) \
        .all()

    return [badge.to_tuple() for badge in badges]


def get_badges_for_users(user_ids):
    """Return all badges that have been awarded to the users, indexed
    by user ID.
    """
    awardings = BadgeAwarding.query \
        .filter(BadgeAwarding.user_id.in_(user_ids)) \
        .options(
            db.joinedload(BadgeAwarding.badge),
        ) \
        .all()

    badges_by_user_id = defaultdict(set)
    for awarding in awardings:
        badge = awarding.badge.to_tuple()
        badges_by_user_id[awarding.user_id].add(badge)

    return dict(badges_by_user_id)


def get_all_badges():
    """Return all badges."""
    badges = Badge.query.all()

    return [badge.to_tuple() for badge in badges]


def award_badge_to_user(badge_id, user_id):
    """Award the badge to the user."""
    awarding = BadgeAwarding(badge_id, user_id)

    db.session.add(awarding)
    db.session.commit()

    return awarding.to_tuple()

# -*- coding: utf-8 -*-

"""
byceps.services.user_badge.service
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

from collections import defaultdict

from ...database import db

from .models import Badge, BadgeAwarding


def create_badge(label, image_filename, *, brand_id=None, description=None):
    """Introduce a new badge."""
    badge = Badge(label, image_filename, brand_id=brand_id,
                  description=description)

    db.session.add(badge)
    db.session.commit()

    return badge.to_tuple()


def find_badge(badge_id):
    """Return the badge with that id, or `None` if not found."""
    badge = Badge.query.get(badge_id)

    return badge.to_tuple()


def get_badges_for_user(user_id):
    """Return all badges that have been awarded to the user."""
    badges = Badge.query \
        .join(BadgeAwarding).filter_by(user_id=user_id) \
        .all()

    return [badge.to_tuple() for badge in badges]


def get_badges_for_users(user_ids):
    """Return all badges that have been awarded to the users, indexed
    by user ID.
    """
    awardings = BadgeAwarding.query \
        .filter(BadgeAwarding.user_id.in_(user_ids)) \
        .options(
            db.joinedload(BadgeAwarding.badge),
        ) \
        .all()

    badges_by_user_id = defaultdict(set)
    for awarding in awardings:
        badge = awarding.badge.to_tuple()
        badges_by_user_id[awarding.user_id].add(badge)

    return dict(badges_by_user_id)


def get_all_badges():
    """Return all badges."""
    badges = Badge.query.all()

    return [badge.to_tuple() for badge in badges]


def award_badge_to_user(badge_id, user_id):
    """Award the badge to the user."""
    awarding = BadgeAwarding(badge_id, user_id)

    db.session.add(awarding)
    db.session.commit()

    return awarding.to_tuple()


def get_awardings_of_badge(badge_id):
    """Return the awardings (user and date) of this badge."""
    awardings = BadgeAwarding.query \
        .filter_by(badge_id=badge_id) \
        .all()

    return [awarding.to_tuple() for awarding in awardings]

Move service function