from spotify_shortcuts.config import Config, load_config
from spotify_shortcuts.shortcut import Shortcut
from spotify_shortcuts.spotify_auth import authenticated_session
from itertools import chain


def all_scopes() -> list[str]:
    from spotify_shortcuts.registry import SHORTCUT_REGISTRY

    return list(
        set(
            chain.from_iterable(
                shortcut.get_scopes() for shortcut in SHORTCUT_REGISTRY.values()
            )
        )
    )


def run_shortcut(shortcut: Shortcut, config: Config):
    client = authenticated_session(
        config, scopes=all_scopes()  # use all scopes to avoid re-authentication
    )

    shortcut.execute(client, config)


def main():
    from spotify_shortcuts.registry import SHORTCUT_REGISTRY
    import sys

    if len(sys.argv) < 2:
        print(f"Usage: {sys.argv[0]} <shortcut_name>")
        sys.exit(1)

    shortcut_name = sys.argv[1]
    if shortcut_name not in SHORTCUT_REGISTRY:
        print(f"Shortcut '{shortcut_name}' not found.")
        sys.exit(1)

    shortcut = SHORTCUT_REGISTRY[shortcut_name]
    config = load_config(args=sys.argv[2:])

    run_shortcut(shortcut, config)


if __name__ == "__main__":
    main()
