"""
Practice: pagination and breaking schema changes. Lab 06's logic for scripts 02_pagination.py
and 06_contracts.py (pagination.py and compat.py): page by offset and by cursor, page through a
stack that changes while you read and count what went wrong, and see why an old Protobuf reader
finds a total of 0 without an error.

Fill in the TODOs, then run the tests:
    pytest test_page_practice.py        (locally)
or press "Run tests" on the chapter page.
"""
import random


def offset_page(stack, k, size=100):
    """Page k (0 = the first) by offset: skip k * size orders from the top, take size.
    stack: order ids, newest first."""
    # TODO
    raise NotImplementedError


def cursor_page(stack, last_seen, size=100):
    """The next page by cursor: the size newest orders with an id lower than last_seen.
    last_seen is None for the first page."""
    # TODO
    raise NotImplementedError


def duplicates_and_skips(pages, present_throughout, low, high):
    """The lab's counter. pages: the ids on each page, in order. present_throughout: ids that
    existed for the whole run. low..high: the id range the reader covered.
    Return {"rows_returned", "duplicates", "skips"}: a duplicate is an id seen twice; a skip is
    an id present the whole time, inside the range, never seen."""
    # TODO
    raise NotImplementedError


def paging_run(method, inserts, deletes, n_pages=50, size=100, first=9_980_001, last=10_000_000, seed=3):
    """The lab's Exercise 2 on a list instead of a database. Start with ids first..last, newest
    first. For each of n_pages pages: read a page ("offset" or "cursor"), then add `inserts` new
    orders on top (ids last + 1, last + 2, ...), then remove `deletes` orders the reader has already
    seen and that are still there, picked with random.Random(seed).sample, as the lab does.
    Return duplicates_and_skips(pages with only the ids that were there at the start,
    those ids minus the removed ones, the lowest id returned, last)."""
    # TODO
    raise NotImplementedError


def expected_errors(n_pages, inserts, deletes):
    """Offset's errors, predicted: every gap between two pages shifts the stack by
    inserts - deletes. Return (duplicates, skips)."""
    # TODO
    raise NotImplementedError


def read_varint(data, pos):
    """Read one Protobuf varint at data[pos]: 7 bits per byte, low bits first; a byte with the
    top bit set means another byte follows. Return (value, position after it)."""
    # TODO
    raise NotImplementedError


def read_whole_number(data, field):
    """What an old reader finds in box `field` if its form says that box holds a whole number
    (a varint). Walk the message: each value starts with a tag varint, tag = field * 8 + wire
    type. Wire type 0: a varint. 2: a varint length, then that many bytes. 1: 8 bytes. 5: 4 bytes.
    Skip anything that isn't (field, type 0). No such value: 0, the default, and no error."""
    # TODO
    raise NotImplementedError


def breaking_json(old, new):
    """A response checker, as compat.json_schema_changes does it for old clients reading new
    responses. old, new: {field name: set of JSON types it can have}. Return a sorted list of
    problems: "removed: <name>" for a field the old schema had and the new one doesn't, and
    "type changed: <name>" when the new schema allows a type the old one didn't. A new field is
    fine (old clients ignore it)."""
    # TODO
    raise NotImplementedError
