from django.db import migrations


FRENCH_TEXT = """Cher utilisateur,

Votre mot de passe à usage unique (OTP) est :

{otp}

Ce code expire dans 10 minutes.

Si vous n’êtes pas à l’origine de cette demande, veuillez ignorer cet e-mail.

Cordialement,
L’équipe de sécurité Wile
"""


FRENCH_HTML = """
<html>
<body style="font-family: Arial; background:#f4f7fb; padding:40px;">
    <div style="max-width:500px; margin:auto; background:#fff; padding:30px; border-radius:10px;">
        <h2 style="color:#0f766e;">Vérification Wile</h2>

        <p>Cher utilisateur,</p>

        <p>Votre OTP est :</p>

        <h1 style="letter-spacing:6px; text-align:center;">{otp}</h1>

        <p>Ce code expire dans 10 minutes.</p>

        <p>Ignorez cet e-mail si vous n’êtes pas à l’origine de la demande.</p>

        <p><strong>L’équipe de sécurité Wile</strong></p>
    </div>
</body>
</html>
"""


def forwards(apps, schema_editor):
    EmailTemplate = apps.get_model("notification_engine", "EmailTemplate")
    EmailTemplateTranslation = apps.get_model("notification_engine", "EmailTemplateTranslation")
    Language = apps.get_model("core", "Language")

    french = Language.objects.get(code="fr")

    for template in EmailTemplate.objects.all():

        # get English subject (important for consistency)
        en_translation = EmailTemplateTranslation.objects.filter(
            template=template
        ).first()

        subject = en_translation.subject if en_translation else template.code

        obj, created = EmailTemplateTranslation.objects.get_or_create(
            template=template,
            language=french,
            defaults={
                "subject": subject,
                "text_message": FRENCH_TEXT,
                "html_message": FRENCH_HTML,
                "is_active": True,
            }
        )

        if not created:
            obj.text_message = FRENCH_TEXT
            obj.html_message = FRENCH_HTML
            obj.save()


def backwards(apps, schema_editor):
    pass


class Migration(migrations.Migration):

    dependencies = [
        ("notification_engine", "0005_french_translation_email"),
    ]

    operations = [
        migrations.RunPython(forwards, backwards),
    ]
