import sys

from bowler import Query
from bowler.imr import FunctionArgument, FunctionSpec
from fissix.fixer_util import Name


def addActivityDefaultValue(node, capture, filename):
  if 'PaypalService.py' in filename:
    # XXX moreorless.patch.PatchException: Failed to apply with offset at 103
    return False

  spec = FunctionSpec.build(node, capture)
  for arg in spec.arguments:
    if arg.name == 'activity':
      return False
    if arg.star:
      print(f"Warning: skipping because ** used: {filename}:{arg.star.lineno}")
      return False

  value = Name("'SQLDict'")
  prefix = None
  # try to keep indentation for multi lines arguments
  if spec.arguments and spec.arguments[-1].prefix:
    prefix = spec.arguments[0].prefix
  fa = FunctionArgument('activity', value, '')
  spec.arguments.insert(0, fa)
  spec.arguments[0].prefix = prefix
  spec.explode()
  return True
  
(
    Query(sys.argv[1], python_version=2)
    .select_method('activate')
    .is_call()
    .filter(addActivityDefaultValue)
    .write()
)