from django.core.management.base import BaseCommand
from datetime import datetime
from BaseModels.mailSender import techSendMail
from ...funcs_options import get_options_by_opt_types, get_mail_send_options

class Command(BaseCommand):

    def handle(self, *args, **options):
        mail_sets = get_mail_send_options()

        log = ''
        log_begin_DT = datetime.now()
        msg = str(log_begin_DT)
        print('-------------')
        print(msg)

        try:
            from RoutesApp.search_matches import search_matches
            msg = search_matches()
            if msg:
                print(msg)
                log += f'\n{msg}'
        except Exception as e:
            msg = f'every_1hour_start search_matches fail = {str(e)}'
            print(msg)
            techSendMail(mail_sets, msg, title='every_1hour_start search_matches')


        if datetime.now().hour == 19:
            try:
                from SubscribesApp.reports import send_mail_for_user_subscribes_that_is_going_to_finish
                msg = send_mail_for_user_subscribes_that_is_going_to_finish()
                if msg:
                    print(msg)
                    log += f'\n{msg}'
            except Exception as e:
                msg = f'send_mail_for_user_subscribes_that_is_going_to_finish search_matches fail = {str(e)}'
                print(msg)
                techSendMail(mail_sets, msg, title='every_1hour_start send_mail_for_user_subscribes_that_is_going_to_finish')

            if log:
                techSendMail(mail_sets, str(msg), title='every_1hour_start get_competitors_prices')



        print(f'- processing time = {str(datetime.now() - log_begin_DT)} -')


