"""Enumerate a shop owner's memberaudit asset sources for the stockroom picker."""

from memberaudit.models import Character, CharacterAsset


def owner_characters(user):
    """memberaudit Characters owned by `user`."""
    return Character.objects.filter(
        eve_character__character_ownership__user=user
    ).select_related("eve_character")


def container_name_for(character_id, item_id) -> str:
    """Display name of a character's container asset (custom name, else type name)."""
    if not item_id:
        return ""
    asset = (
        CharacterAsset.objects.filter(character_id=character_id, item_id=item_id)
        .select_related("eve_type").first()
    )
    if asset is None:
        return ""
    return asset.name or asset.eve_type.name


def source_tree(user):
    """Nested [{character_id, character_name, stations:[{location_id, name, containers:[{item_id, name}]}]}]."""
    tree = []
    for char in owner_characters(user):
        top = list(
            CharacterAsset.objects.filter(
                character=char, parent__isnull=True, location__isnull=False
            ).select_related("location", "eve_type")
        )
        # Containers = top-level assets that HOLD items and are NOT ships
        # (a fitted ship also "has children" — its modules — so exclude category 6).
        container_ids = set(
            CharacterAsset.objects.filter(
                character=char, parent__isnull=True, children__isnull=False
            )
            .exclude(eve_type__eve_group__eve_category_id=6)
            .values_list("id", flat=True)
        )
        stations = {}
        for asset in top:
            st = stations.setdefault(
                asset.location_id,
                {"location_id": asset.location_id, "name": asset.location.name, "containers": []},
            )
            if asset.id in container_ids:
                st["containers"].append({"item_id": asset.item_id, "name": asset.name or asset.eve_type.name})
        for st in stations.values():
            st["containers"].sort(key=lambda c: c["name"].lower())
        if stations:
            tree.append({
                "character_id": char.id,
                "character_name": char.eve_character.character_name,
                "stations": sorted(stations.values(), key=lambda s: s["name"].lower()),
            })
    return tree
