#!/usr/bin/python3
import json
import sys

from catalog_update.upgrade_strategy import datetime_versioning


def newer_mapping(image_tags):
    key = list(image_tags.keys())[0]
    tags = {t.strip('RELEASE.'): t for t in image_tags[key] if t.startswith('RELEASE.') and t.endswith('Z')}
    version = datetime_versioning(list(tags), '%Y-%m-%dT%H-%M-%SZ')
    if not version:
        return {}

    return {
        'tags': {key: tags[version]},
        'app_version': version.split('T')[0],
    }


if __name__ == '__main__':
    try:
        versions_json = json.loads(sys.stdin.read())
    except ValueError:
        raise ValueError('Invalid json specified')

    print(json.dumps(newer_mapping(versions_json)))
