# Generated by Django 4.2.2 on 2026-04-25 09:14
from django.db import migrations


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

    templates = {
        "CUSTOMER_REGISTER_OTP": {
            "en": {
                "subject": "{app_name} - Account verification OTP",
                "text": "Your OTP is {otp}. Do not share it with anyone.",
                "html": """
                <html>
                <body>
                    <h2>Verify your account</h2>
                    <p>Your OTP is:</p>
                    <h1>{otp}</h1>
                    <p>Do not share this code with anyone.</p>
                    <p>{app_name} Security Team</p>
                </body>
                </html>
                """,
            }
        },
        "CUSTOMER_RESEND_OTP": {
            "en": {
                "subject": "{app_name} - New verification OTP",
                "text": "Your new OTP is {otp}.",
                "html": """
                <html>
                <body>
                    <h2>New OTP</h2>
                    <p>Your OTP is:</p>
                    <h1>{otp}</h1>
                    <p>Do not share this code with anyone.</p>
                    <p>{app_name} Security Team</p>
                </body>
                </html>
                """,
            }
        },
        "CUSTOMER_LOGIN_VERIFY_OTP": {
            "en": {
                "subject": "{app_name} - Login verification OTP",
                "text": "Your login OTP is {otp}.",
                "html": """
                <html>
                <body>
                    <h2>Login verification</h2>
                    <p>Your OTP is:</p>
                    <h1>{otp}</h1>
                    <p>Do not share this code with anyone.</p>
                    <p>{app_name} Security Team</p>
                </body>
                </html>
                """,
            }
        },
        "CUSTOMER_FORGOT_PASSWORD_OTP": {
            "en": {
                "subject": "{app_name} - Password reset OTP",
                "text": "Your reset OTP is {otp}.",
                "html": """
                <html>
                <body>
                    <h2>Password reset</h2>
                    <p>Your OTP is:</p>
                    <h1>{otp}</h1>
                    <p>Do not share this code with anyone.</p>
                    <p>{app_name} Security Team</p>
                </body>
                </html>
                """,
            }
        },
    }

    for template_code, translations in templates.items():
        template, _ = EmailTemplate.objects.get_or_create(
            code=template_code,
            defaults={
                "is_active": True,
                "default_from_email": "no-reply@farmcomp.fi",
            }
        )

        for lang_code, content in translations.items():
            language = Language.objects.filter(code=lang_code).first()

            if not language:
                continue

            EmailTemplateTranslation.objects.update_or_create(
                template=template,
                language=language,
                defaults={
                    "subject": content["subject"],
                    "text_message": content["text"],
                    "html_message": content["html"],
                    "is_active": True,
                }
            )


def reverse_seed_email_templates(apps, schema_editor):
    EmailTemplate = apps.get_model("notification_engine", "EmailTemplate")

    EmailTemplate.objects.filter(
        code__in=[
            "CUSTOMER_REGISTER_OTP",
            "CUSTOMER_RESEND_OTP",
            "CUSTOMER_LOGIN_VERIFY_OTP",
            "CUSTOMER_FORGOT_PASSWORD_OTP",
        ]
    ).delete()


class Migration(migrations.Migration):

    dependencies = [
        ('notification_engine', '0002_seed_email_templates'),
    ]

    operations = [
        migrations.RunPython(
            seed_email_templates,
            reverse_seed_email_templates
        ),
    ]
