#!/usr/bin/env python3
from __future__ import annotations

import argparse
import getpass
import json
import subprocess
from typing import Sequence


def remote_upstream(args_remote: str, args_upstream: str) -> tuple[str, str]:
    cmd = ('git', 'remote')
    remotes = subprocess.check_output(cmd, encoding='UTF-8').splitlines()

    if args_remote and args_remote in remotes:
        remote = args_remote
    elif getpass.getuser() in remotes:
        remote = getpass.getuser()
    elif 'origin' in remotes:
        remote = 'origin'
    else:
        raise AssertionError(f'Which remote? {remotes}')

    if args_upstream and args_upstream in remotes:
        upstream = args_upstream
    elif 'upstream' in remotes:
        upstream = 'upstream'
    elif 'origin' in remotes:
        upstream = 'origin'
    else:
        raise AssertionError(f'Which upstream? {remotes}')

    return (remote, upstream)


def main(argv: Sequence[str] | None = None) -> int:
    parser = argparse.ArgumentParser()
    parser.add_argument('--remote')
    parser.add_argument('--upstream')
    args = parser.parse_args(argv)
    print(json.dumps(remote_upstream(args.remote, args.upstream)))
    return 0


if __name__ == '__main__':
    raise SystemExit(main())
