#!/usr/bin/env python3
"""Legt einen POI für eine Route an."""

import argparse
from collections.abc import Sequence

from common import add_common_arguments, ask, client_from_args, run


def build_parser() -> argparse.ArgumentParser:
    parser = argparse.ArgumentParser(description="POI anlegen")
    add_common_arguments(parser)
    parser.add_argument("--route-id", type=int)
    parser.add_argument("--title")
    parser.add_argument("--description")
    parser.add_argument("--lat", type=float)
    parser.add_argument("--lon", type=float)
    parser.add_argument("--trigger-radius", type=float)
    parser.add_argument("--sequence", type=int)
    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)
    body = {
        "title": ask("Titel", args.title),
        "description": ask("Beschreibung", args.description),
        "lat": ask("Breitengrad", args.lat, required=True, cast=float),
        "lon": ask("Längengrad", args.lon, required=True, cast=float),
        "triggerRadiusM": ask("Auslöseradius in Metern", args.trigger_radius, cast=float),
        "sequence": ask("Reihenfolge", args.sequence, cast=int),
    }
    payload = {key: value for key, value in body.items() if value is not None}
    return client.request("POST", f"/api/routes/{route_id}/pois", json_body=payload)


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