# Generated by Django 4.2.16 on 2025-02-03 09:52

from django.db import migrations
from utils import migration_utils


def update_password_reset(apps, schema_editor):
    values_to_replace = [
        "<p>Dear {{ reset_token.account.full_name }},<br/><br/>A password reset for your account has been requested. You can reset your password at the following link:</p><p>{{ core_reset_password_url }}</p>",
    ]
    replacement_value = '<p>Dear {{ reset_token.account.full_name }},<br/><br/>A password reset for your account has been requested. You can reset your password at the following link:</p><p><a href="{{ core_reset_password_url }}">{{ core_reset_password_url }}</a></p>'

    migration_utils.update_default_setting_values(
        apps,
        setting_name="password_reset",
        group_name="email",
        values_to_replace=values_to_replace,
        replacement_value=replacement_value,
    )


def update_new_user_registration(apps, schema_editor):
    values_to_replace = [
        "<p>Dear {{ user.full_name }}<br/><br/>Thank you for registering for {{ request.journal.name }}. You can confirm your account at the following link:</p><p><br/>{{ core_confirm_account_url }} <br/></p>",
    ]
    replacement_value = '<p>Dear {{ user.full_name }}<br/><br/>Thank you for registering for {{ site_name }}. You can confirm your account at the following link:</p><p><a href="{{ core_confirm_account_url }}">{{ core_confirm_account_url }}</a></p>'

    migration_utils.update_default_setting_values(
        apps,
        setting_name="new_user_registration",
        group_name="email",
        values_to_replace=values_to_replace,
        replacement_value=replacement_value,
    )


def update_user_email_change(apps, schema_editor):
    values_to_replace = [
        "<p>Dear {{ user.full_name }},</p><p>You have requested that your email address be updated.</p><p>You can confirm your account at the following link: confirm account: {{ core_confirm_account_url }} </p><p>Regards,</p>",
    ]
    replacement_value = '<p>Dear {{ user.full_name }},</p><p>You have requested that your email address be updated.</p><p>You can confirm your account at the following ink:</p><p><a href="{{ core_confirm_account_url }}">{{ core_confirm_account_url }}</a></p><p>Regards,</p>'

    migration_utils.update_default_setting_values(
        apps,
        setting_name="user_email_change",
        group_name="email",
        values_to_replace=values_to_replace,
        replacement_value=replacement_value,
    )


class Migration(migrations.Migration):
    dependencies = [
        ("utils", "0039_upgrade_1_7_4"),
    ]

    operations = [
        migrations.RunPython(
            update_password_reset,
            reverse_code=migrations.RunPython.noop,
        ),
        migrations.RunPython(
            update_new_user_registration,
            reverse_code=migrations.RunPython.noop,
        ),
        migrations.RunPython(
            update_user_email_change,
            reverse_code=migrations.RunPython.noop,
        ),
    ]
