from .base import BaseTeamsnapObject
from .eventlineup import EventLineup
import apiclient.exceptions


class EventLineupEntry(BaseTeamsnapObject):
    rel = "event_lineup_entries"
    type = "event_lineup_entry"
    version = "3.866.0"

    __slots__ = [
        "event_lineup_id",
        "event_id",
        "member_id",
        "sequence",
        "label",
        "member_name",
        "member_photo",
        "availability_status_code"
    ]

    @classmethod
    def search(cls, client, **kwargs):
        # For some reason the query listed for search at this endpoint is for EventLineup, not EventLineupEntry
        # this is a workaround, specifically line 31 which calls class method instead of client
        try:
            event_lineup_results = client.query(EventLineup.rel, "search", **kwargs)
            event_lineups = [EventLineup(client, data=result) for result in event_lineup_results]
            lineup_entries = []
            for event_lineup in event_lineups:
                event_lineup_entries_results = cls.query(client, "search", event_lineup_id=event_lineup.id)
                lineup_entries += [cls(client, data=result) for result in event_lineup_entries_results]
        except apiclient.exceptions.ServerError as e:
            raise e

        return lineup_entries


    @classmethod
    def query(cls, client, query: str, **kwargs) -> list:
        # For some reason the query listed for search at this endpoint is for EventLineup, not EventLineupEntry
        # this is a workaround

        if cls.rel == "event_lineup_entries" and query == "search":
            response = client.get(
                f"https://api.teamsnap.com/v3/{cls.rel}/{query}",
                params=kwargs)
            parsed_response = [{d.name: d.value for d in item.data} for item in response.items]
            return parsed_response

        else:
            return client.query(cls.rel, query, **kwargs)
