# -*- coding: utf-8 -*-
"""Which steps the guide goes through, and where Další and Zpět lead (docs/plan/onboarding/000-plan.md D29-D31, D33).

Pure logic, no Kodi: the window (wizard.py) asks it with what it knows. The path is what the user really walks:
Vítej, the account step when they chose the account, the key steps not already satisfied, ChatGPT (D39, only what it
does: it is connected on the web) and the questionnaire; a
guide finished before (only the TMDb key is missing, D33) is Vítej and the key alone. Zpět never returns to the account
step: the account is chosen on Vítej only (D29, D31)."""

KEYS = ('tmdb', 'webshare')                             # no AI key step (D39): next to ChatGPT it confused
CHATGPT = 'chatgpt'                                     # D39: what Milionář in ChatGPT does; it is connected on the web
TASTE = ('genres', 'movies', 'series')
ORDER = ('welcome', 'account') + KEYS + ('ai', CHATGPT) + TASTE   # 'ai' only places a step stored by an older guide


def path(account, skipped, finished):
    """account: the account step is part of this run; skipped: satisfied key steps; finished: no questionnaire."""
    steps = ['welcome']
    if account:
        steps.append('account')
    if finished:                                         # only the key is missing (D33): nothing optional again
        return steps + [step for step in ('tmdb',) if step not in skipped]
    return steps + [step for step in KEYS if step not in skipped] + [CHATGPT] + list(TASTE)


def _position(steps, current):
    """Index of `current` in `steps`, or where it would stand (a step that has just dropped out of the path)."""
    if current in steps:
        return steps.index(current), True
    rank = ORDER.index(current)
    before = [i for i, step in enumerate(steps) if ORDER.index(step) < rank]
    return (before[-1] if before else -1), False


def following(steps, current):
    """The step after `current`, or None at the end of the path."""
    index, _found = _position(steps, current)
    return steps[index + 1] if index + 1 < len(steps) else None


def previous(steps, current):
    """The step before `current` (never the account step), or None on Vítej."""
    index, found = _position(steps, current)
    candidates = steps[:index] if found else steps[:index + 1]
    for step in reversed(candidates):
        if step != 'account':
            return step
    return None


def progress(steps, current):
    """(N, M) for „Krok N z M“."""
    index, found = _position(steps, current)
    return (index + 1 if found else index + 2), len(steps)
