#!/usr/bin/env python3

import argparse
import os
import os.path
import sys

from decimal import Decimal
from solana.publickey import PublicKey

sys.path.insert(0, os.path.abspath(
    os.path.join(os.path.dirname(__file__), "..")))
import mango  # nopep8

parser = argparse.ArgumentParser(
    description="Sends a notification if an account's SOL balance is below the '--minimum-sol-balance' parameter threshold.")
mango.ContextBuilder.add_command_line_parameters(parser)
parser.add_argument("--address", type=PublicKey, required=True,
                    help="address of the account")
parser.add_argument("--minimum-sol-balance", type=Decimal, default=Decimal("0.1"),
                    help="the minimum SOL balance required for the alert. A SOL balance less than this value will trigger a nifitication.")
parser.add_argument("--notify", type=mango.parse_notification_target, action="append", default=[],
                    help="The notification target for low balance events")
args: argparse.Namespace = mango.parse_args(parser)

context = mango.ContextBuilder.from_command_line_parameters(args)

account_info = mango.AccountInfo.load(context, args.address)
if account_info is None:
    raise Exception(f"No account at '{args.address}'")
else:
    if account_info.sols < args.minimum_sol_balance:
        notify: mango.NotificationTarget = mango.CompoundNotificationTarget(args.notify)
        report = f"Account \"{args.name} [{args.address}]\" on {context.client.cluster_name} has only {account_info.sols} SOL, which is below the minimum required balance of {args.minimum_sol_balance} SOL."
        notify.send(report)
        print(f"Notification sent: {report}")
