#!python

from desitarget.mtl import force_overrides
from desiutil.log import get_logger
log = get_logger()

# ADM default survey to run.
survey = "main"

from argparse import ArgumentParser
ap = ArgumentParser(description='Force override ledgers to be processed and added to the MTL ledgers without running the full MTL loop.')
ap.add_argument("obscon",
                help="String matching ONE obscondition in the bitmask yaml file \
                (e.g. 'BRIGHT'). Controls priorities when merging targets,      \
                which tiles to process, etc.", choices=["DARK", "BRIGHT", "BACKUP"])
ap.add_argument("-s", "--survey",
                help="Flavor of survey to run. Defaults to [{}]".format(survey),
                default=survey, choices=["main", "sv3", "sv2"])
ap.add_argument('--mtldir',
                help="Full path to the directory that hosts the MTL ledgers.    \
                Default is to use the $MTL_DIR environment variable",
                default=None)
ap.add_argument("-sec", "--secondary", action='store_true',
                help="Pass if overrides are being forced into secondary, rather \
                than primary ledgers.")
ap.add_argument("-p", "--pixlist",
                help="A list of HEALPixels signifying the ledgers to be updated.\
                Send as a comma-separated string (e.g. 12167,53,455,9). The     \
                default is to run all possible pixels.",
                default=None)
ap.add_argument("--test", action="store_true",
                help="In general, when we force overrides we want to do so for  \
                all pixels so the Alternate MTLs can do the same. Therefore, to \
                have a higher bar, this must be passed with pixlist.")

ns = ap.parse_args()

hpxlist = ns.pixlist
if ns.pixlist is not None:
    hpxlist = [ pix for pix in ns.pixlist.split(',') ]
    if not ns.test:
        msg = "Passing a list of pixels is only for testing purposes!!!"
        log.critical(msg)
        raise ValueError

outdir = force_overrides(ns.obscon, survey=ns.survey, secondary=ns.secondary,
                         mtldir=ns.mtldir, pixlist=hpxlist)

log.info("Overrode ledgers in {}".format(outdir))
