import argparse
import json
from utils import generate_base_nexus_upload_path
from actions_logging.app_logging import logger

if __name__ == "__main__":
    # Parse command-line arguments
    parser = argparse.ArgumentParser(description="Get input parameters")
    parser.add_argument('--manifest_file', type=str, help='Firefly manifest file path', required=True)
    parser.add_argument('--target_manifest_file', type=str, help='New manifest file path', required=True)
    parser.add_argument('--version', type=str, help='Firefly version', required=True)
    parser.add_argument('--branch', type=str, help='From which branch is the build accuring', required=True)
    parser.add_argument('--gha_run_number', type=str, help='The run number GH has assigned to the build', required=True)
    parser.add_argument('--release', type=str, help='Is this a release build', required=True)
    parser.add_argument('--linux_hash', type=str, help='firefly.so sha256', required=True)
    parser.add_argument('--windows_hash', type=str, help='firefly.dll sha256', required=True)
    parser.add_argument('--mac_hash', type=str, help='firefly.dylib sha256', required=True)

    args = parser.parse_args()
    if args.release == "true" or args.release == "True" or args.release == "release":
        release = True
    else:
        release = False

    with open(args.manifest_file, "r") as manifest_file:
        manifest_data = json.load(manifest_file)

    new_manifest = {
        "name": "Firefly",
        "version": args.version,
        "source": generate_base_nexus_upload_path(args.version, args.branch, args.gha_run_number, release),
        "platforms": [
            {
                "os": "Linux",
                "arch": "X64",
                "source": "Linux/X64/firefly.so",
                "hash": args.linux_hash
            },
            {
                "os": "Windows",
                "arch": "X64",
                "source": "Windows/X64/firefly.dll",
                "hash": args.windows_hash
            },
            {
                "os": "macOS",
                "arch": "unified",
                "source": "macOS/unified/firefly.dylib",
                "hash": args.mac_hash
            }
        ],
        "dependencies": manifest_data["dependencies"],
    }

    logger.info_green(f"New manifest: {json.dumps(new_manifest, indent=4)}")

    with open(args.target_manifest_file, "w") as manifest_file:
        json.dump(new_manifest, manifest_file, indent=4)
