# Path: countylimits/data_collection/gather_county_data.py
# def get_chums_data(year=None):
#     """
#     Downloads and processes mortgage data files for the next year.
# 
#     Normally, updates are run in December preceding the new data year,
#     so the default year is current year + 1.
#     If updates need to be run for the current year, or any other year,
#     then pass in your desired 'year' value.
# 
#     Files are available manually
#     at https://www.hud.gov/pub/chums/file_layouts.html
#     """
#     year = year or datetime.date.today().year + 1
#     msg = ""
#     try:
#         fha = download_datafile(CHUMS_FHA_URL.format(year)).split("\r\n")
#         if fha[0].startswith("Error"):
#             msg += fha[0]
#             raise ValueError(fha[0])
#         fha_data = translate_data(fha, CHUMS_MAP)
#         dump_to_csv(
#             "{}/forward_limits_{}.csv".format(CSV_DIR, year),
#             CHUMS_MAP.keys(),
#             fha_data,
#         )
#         msg += "FHA limits saved to {}/forward_limits_{}.csv\n".format(
#             CSV_DIR, year
#         )
#         gse = download_datafile(CHUMS_GSE_URL.format(year)).split("\r\n")
#         if gse[0].startswith("Error"):  # pragma: no cover tested above
#             msg += gse[0]
#             raise ValueError(gse[0])
#         gse_data = translate_data(gse, CHUMS_MAP)
#         gse_file = "{}/gse_limits_{}.csv".format(CSV_DIR, year)
#         dump_to_csv(gse_file, CHUMS_MAP.keys(), gse_data)
#         msg += "GSE limits saved to {}\n".format(gse_file)
#         final_data = assemble_final_data(fha_data, gse_data)
#         yearly_file = "{}/county_limit_data_flat_{}.csv".format(CSV_DIR, year)
#         final_file = "{}/county_limit_data_latest.csv".format(DATA_DIR)
#         dump_to_csv(yearly_file, FINAL_FIELDNAMES, final_data)
#         dump_to_csv(final_file, FINAL_FIELDNAMES, final_data)
#         msg += "Final flat file saved to {}\n".format(final_file)
#         msg += (
#             "All county source files processed.\n"
#             "Data can be loaded with this command: \n"
#             "`python manage.py load_county_limits "
#             "data/county_limit_data_latest.csv --confirm=y`"
#         )
#     except Exception:
#         return "{}\n{}".format(ERROR_MSG, msg)
#     return msg


from django.core.management.base import BaseCommand
from countylimits.data_collection.gather_county_data import get_chums_data



COMMAND_HELP = "Gathers annaul county mortgage limit data from the "
"HUD website and processes it for use in the owning-a-home-api. "
"Processed source files are saved in the app's /data/base_data/ folder.  "
PARSER_HELP = "An optional '--year' value may be supplied to process "
"data files from a particular year. The default year value is the year after "
"the current year, because this script is normally run at the end of "
"December for the next year's values."


class Command(BaseCommand):
    help = COMMAND_HELP

    def add_arguments(self, parser):
        parser.add_argument("--year", help=PARSER_HELP, type=int)

    def handle(self, *args, **options):
        if options["year"]:
            msg = get_chums_data(year=options["year"])
        else:

# Path: countylimits/data_collection/gather_county_data.py
# def get_chums_data(year=None):
#     """
#     Downloads and processes mortgage data files for the next year.
# 
#     Normally, updates are run in December preceding the new data year,
#     so the default year is current year + 1.
#     If updates need to be run for the current year, or any other year,
#     then pass in your desired 'year' value.
# 
#     Files are available manually
#     at https://www.hud.gov/pub/chums/file_layouts.html
#     """
#     year = year or datetime.date.today().year + 1
#     msg = ""
#     try:
#         fha = download_datafile(CHUMS_FHA_URL.format(year)).split("\r\n")
#         if fha[0].startswith("Error"):
#             msg += fha[0]
#             raise ValueError(fha[0])
#         fha_data = translate_data(fha, CHUMS_MAP)
#         dump_to_csv(
#             "{}/forward_limits_{}.csv".format(CSV_DIR, year),
#             CHUMS_MAP.keys(),
#             fha_data,
#         )
#         msg += "FHA limits saved to {}/forward_limits_{}.csv\n".format(
#             CSV_DIR, year
#         )
#         gse = download_datafile(CHUMS_GSE_URL.format(year)).split("\r\n")
#         if gse[0].startswith("Error"):  # pragma: no cover tested above
#             msg += gse[0]
#             raise ValueError(gse[0])
#         gse_data = translate_data(gse, CHUMS_MAP)
#         gse_file = "{}/gse_limits_{}.csv".format(CSV_DIR, year)
#         dump_to_csv(gse_file, CHUMS_MAP.keys(), gse_data)
#         msg += "GSE limits saved to {}\n".format(gse_file)
#         final_data = assemble_final_data(fha_data, gse_data)
#         yearly_file = "{}/county_limit_data_flat_{}.csv".format(CSV_DIR, year)
#         final_file = "{}/county_limit_data_latest.csv".format(DATA_DIR)
#         dump_to_csv(yearly_file, FINAL_FIELDNAMES, final_data)
#         dump_to_csv(final_file, FINAL_FIELDNAMES, final_data)
#         msg += "Final flat file saved to {}\n".format(final_file)
#         msg += (
#             "All county source files processed.\n"
#             "Data can be loaded with this command: \n"
#             "`python manage.py load_county_limits "
#             "data/county_limit_data_latest.csv --confirm=y`"
#         )
#     except Exception:
#         return "{}\n{}".format(ERROR_MSG, msg)
#     return msg


# Path: countylimits/management/commands/gather_limit_data.py
from django.core.management.base import BaseCommand
from countylimits.data_collection.gather_county_data import get_chums_data




COMMAND_HELP = "Gathers annaul county mortgage limit data from the "
"HUD website and processes it for use in the owning-a-home-api. "
"Processed source files are saved in the app's /data/base_data/ folder.  "
PARSER_HELP = "An optional '--year' value may be supplied to process "
"data files from a particular year. The default year value is the year after "
"the current year, because this script is normally run at the end of "
"December for the next year's values."


class Command(BaseCommand):
    help = COMMAND_HELP

    def add_arguments(self, parser):
        parser.add_argument("--year", help=PARSER_HELP, type=int)

    def handle(self, *args, **options):
        if options["year"]:
            msg = get_chums_data(year=options["year"])
        else:

            msg = get_chums_data()