from .models import *
from datetime import datetime, timedelta
from .funcs import *
from django.utils.translation import gettext as _

def send_mail_for_user_subscribes_that_is_going_to_finish():

    log = ''

    user_subscribes = get_user_subscribes_that_is_going_to_finish()

    for user_subscribe in user_subscribes:
        if not user_subscribe.receive_finish_subscribe_msg:
            continue

        try:

            from GeneralApp.funcs_options import get_options_by_opt_types, get_mail_send_options
            sets = get_options_by_opt_types(['domain', 'project_name'], only_vals=True)

            subject = _('tripwb.com - Уведомление о скором окончании срока подписки')

            Dict = {
                'logo': f'{sets["domain"]}/static/img/svg/LogoMobile.svg',
                'message_title': subject,
                'user_subscribe': user_subscribe
            }
            Dict.update(sets)

            html = render_to_string('mail/m_user_subscribes_that_is_going_to_finish.html', Dict)
            from BaseModels.mailSender import admin_send_mail_by_SMTPlib
            mail_sets = get_mail_send_options()
            to = [user_subscribe.user.email, 'web@syncsystems.net', 'sa@a3-global.com', 'sysadmin.hax@gmail.com']
            res = admin_send_mail_by_SMTPlib(
                mail_sets,
                subject=subject,
                from_email=mail_sets['sender_email'], to=to,
                html_content=html
            )
            if res and type(res) == str:
                print(res)
                # log += f'\n{res}'


        except Exception as e:
            msg = (f'send_mail_for_user_subscribes_that_is_going_to_finish '
                   f'for user {user_subscribe.user} '
                   f'Exception = {str(e)}')
            print(msg)
            log += f'\n{msg}'

    return log