#!/usr/bin/env python3
"""Ändert Routendaten und ersetzt optional die GPX-Datei."""

import argparse
from collections.abc import Sequence

from common import add_common_arguments, ask, ask_path, ask_with_default, client_from_args, run


def build_parser() -> argparse.ArgumentParser:
    parser = argparse.ArgumentParser(description="Wegwichtel-Route ändern")
    add_common_arguments(parser)
    parser.add_argument("--route-id", type=int)
    parser.add_argument("--gpx")
    return parser


def main(argv: Sequence[str] | None = None):
    args = build_parser().parse_args(argv)
    client = client_from_args(args)
    route_id = ask("Route-ID", args.route_id, required=True, cast=int)
    path = f"/api/routes/{route_id}"
    current = client.request("GET", path)
    fields = {
        "name": ask_with_default("Name", current.get("name", "")),
        "description": ask_with_default("Beschreibung", current.get("description", "")),
        "schoolName": ask_with_default("Schule", current.get("schoolName", "")),
    }
    gpx = ask_path("Neue GPX-Datei (leer: unverändert)", args.gpx, required=False)
    if gpx:
        return client.request("PUT", path, fields=fields, files={"gpx": gpx})
    return client.request("PUT", path, json_body=fields)


if __name__ == "__main__":
    run(main)
