# Path: countylimits/data_collection/county_data_monitor.py
# def check_for_county_changes(email=None):
#     """
#     Check the census county changelog against a local copy of the last log
#     to see whether updates have been added. If changes are detected,
#     note the change and update our local 'last_changelog.txt' file.
#     """
#     current_changelog = get_current_log()
#     current_lines = get_lines(current_changelog)
#     base_lines = get_lines(get_base_log())
#     if base_lines == current_lines:
#         msg = "No county changes found, no emails sent."
#         return msg
#     else:
#         msg = (
#             "County changes need to be checked at {}\n"
#             "These changes were detected:".format(CENSUS_CHANGELOG)
#         )
#         diffsets = []
#         diffset = ndiff(base_lines, current_lines)
#         diffsets.append(
#             d for d in diffset if d.startswith("- ") or d.startswith("+ ")
#         )
#         for diffsett in diffsets:
#             for diff in diffsett:
#                 msg += "\n{}".format(diff)
#         store_change_log(current_changelog)
#         msg += "\n\nOur 'last_changelog.txt' file has been updated."
#         if email:
#             send_mail(
#                 "Owning a Home alert: Change detected in census county data",
#                 msg,
#                 "tech@cfpb.gov",
#                 email,
#                 fail_silently=False,
#             )
# 
#             return (
#                 "Emails were sent to {} with the following message: \n\n"
#                 "{}".format(", ".join(email), msg)
#             )
#         else:
#             return msg


from django.core.management.base import BaseCommand
from countylimits.data_collection.county_data_monitor import (  # noqa
    check_for_county_changes,
)



COMMAND_HELP = "Check the census county changelog against a local copy "
"of the last log to see whether updates have been added. "
"If changes are detected, send an email alert about the change "
"and update our local 'last_changelog.txt' file."
PARSER_HELP = "This command accepts a space-separated string "
"of email recipients who will be notified if county changes are detected."


class Command(BaseCommand):
    help = COMMAND_HELP

    def add_arguments(self, parser):
        parser.add_argument("--email", help=PARSER_HELP, nargs="+", type=str)

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

# Path: countylimits/data_collection/county_data_monitor.py
# def check_for_county_changes(email=None):
#     """
#     Check the census county changelog against a local copy of the last log
#     to see whether updates have been added. If changes are detected,
#     note the change and update our local 'last_changelog.txt' file.
#     """
#     current_changelog = get_current_log()
#     current_lines = get_lines(current_changelog)
#     base_lines = get_lines(get_base_log())
#     if base_lines == current_lines:
#         msg = "No county changes found, no emails sent."
#         return msg
#     else:
#         msg = (
#             "County changes need to be checked at {}\n"
#             "These changes were detected:".format(CENSUS_CHANGELOG)
#         )
#         diffsets = []
#         diffset = ndiff(base_lines, current_lines)
#         diffsets.append(
#             d for d in diffset if d.startswith("- ") or d.startswith("+ ")
#         )
#         for diffsett in diffsets:
#             for diff in diffsett:
#                 msg += "\n{}".format(diff)
#         store_change_log(current_changelog)
#         msg += "\n\nOur 'last_changelog.txt' file has been updated."
#         if email:
#             send_mail(
#                 "Owning a Home alert: Change detected in census county data",
#                 msg,
#                 "tech@cfpb.gov",
#                 email,
#                 fail_silently=False,
#             )
# 
#             return (
#                 "Emails were sent to {} with the following message: \n\n"
#                 "{}".format(", ".join(email), msg)
#             )
#         else:
#             return msg


# Path: countylimits/management/commands/oah_check_county_changes.py
from django.core.management.base import BaseCommand
from countylimits.data_collection.county_data_monitor import (  # noqa
    check_for_county_changes,
)




COMMAND_HELP = "Check the census county changelog against a local copy "
"of the last log to see whether updates have been added. "
"If changes are detected, send an email alert about the change "
"and update our local 'last_changelog.txt' file."
PARSER_HELP = "This command accepts a space-separated string "
"of email recipients who will be notified if county changes are detected."


class Command(BaseCommand):
    help = COMMAND_HELP

    def add_arguments(self, parser):
        parser.add_argument("--email", help=PARSER_HELP, nargs="+", type=str)

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

            msg = check_for_county_changes()