from django.core.management.base import BaseCommand

from stiftung.models import AppConfiguration


class Command(BaseCommand):
    help = "Initialize default app configuration settings"

    def handle(self, *args, **options):
        # E-Mail / IMAP Settings
        email_settings = [
            {
                "key": "imap_host",
                "display_name": "IMAP Server",
                "description": "Hostname oder IP-Adresse des IMAP-Servers (z.B. mail.example.com)",
                "value": "",
                "default_value": "",
                "setting_type": "text",
                "category": "email",
                "order": 1,
            },
            {
                "key": "imap_port",
                "display_name": "IMAP Port",
                "description": "Port des IMAP-Servers (Standard: 993 für SSL, 143 für unverschlüsselt)",
                "value": "993",
                "default_value": "993",
                "setting_type": "number",
                "category": "email",
                "order": 2,
            },
            {
                "key": "imap_user",
                "display_name": "IMAP Benutzername",
                "description": "Benutzername / E-Mail-Adresse für die IMAP-Anmeldung",
                "value": "",
                "default_value": "",
                "setting_type": "text",
                "category": "email",
                "order": 3,
            },
            {
                "key": "imap_password",
                "display_name": "IMAP Passwort",
                "description": "Passwort für die IMAP-Anmeldung",
                "value": "",
                "default_value": "",
                "setting_type": "password",
                "category": "email",
                "order": 4,
            },
            {
                "key": "imap_folder",
                "display_name": "IMAP Ordner",
                "description": "Name des zu überwachenden Postfach-Ordners (Standard: INBOX)",
                "value": "INBOX",
                "default_value": "INBOX",
                "setting_type": "text",
                "category": "email",
                "order": 5,
            },
            {
                "key": "imap_use_ssl",
                "display_name": "SSL/TLS verwenden",
                "description": "Sichere Verbindung zum IMAP-Server (empfohlen)",
                "value": "True",
                "default_value": "True",
                "setting_type": "boolean",
                "category": "email",
                "order": 6,
            },
            # SMTP Settings
            {
                "key": "smtp_host",
                "display_name": "SMTP Server",
                "description": "Hostname des SMTP-Servers (z.B. smtp.ionos.de)",
                "value": "smtp.ionos.de",
                "default_value": "smtp.ionos.de",
                "setting_type": "text",
                "category": "email",
                "order": 10,
            },
            {
                "key": "smtp_port",
                "display_name": "SMTP Port",
                "description": "Port des SMTP-Servers (465 für SSL, 587 für STARTTLS)",
                "value": "465",
                "default_value": "465",
                "setting_type": "number",
                "category": "email",
                "order": 11,
            },
            {
                "key": "smtp_user",
                "display_name": "SMTP Benutzername",
                "description": "Benutzername / E-Mail-Adresse für die SMTP-Anmeldung",
                "value": "",
                "default_value": "",
                "setting_type": "text",
                "category": "email",
                "order": 12,
            },
            {
                "key": "smtp_password",
                "display_name": "SMTP Passwort",
                "description": "Passwort für die SMTP-Anmeldung",
                "value": "",
                "default_value": "",
                "setting_type": "password",
                "category": "email",
                "order": 13,
            },
            {
                "key": "smtp_use_ssl",
                "display_name": "SSL/TLS verwenden (SMTP)",
                "description": "Sichere Verbindung zum SMTP-Server (empfohlen für Port 465)",
                "value": "True",
                "default_value": "True",
                "setting_type": "boolean",
                "category": "email",
                "order": 14,
            },
            {
                "key": "smtp_from_email",
                "display_name": "Absenderadresse (SMTP)",
                "description": "Absenderadresse für ausgehende E-Mails (z.B. buero@vhtv-stiftung.de)",
                "value": "buero@vhtv-stiftung.de",
                "default_value": "buero@vhtv-stiftung.de",
                "setting_type": "text",
                "category": "email",
                "order": 15,
            },
        ]

        all_settings = email_settings

        created_count = 0
        updated_count = 0

        for setting_data in all_settings:
            setting, created = AppConfiguration.objects.get_or_create(
                key=setting_data["key"], defaults=setting_data
            )

            if created:
                created_count += 1
                self.stdout.write(
                    self.style.SUCCESS(f"Created setting: {setting.display_name}")
                )
            else:
                # Update existing setting with new defaults if needed
                if not setting.description:
                    setting.description = setting_data["description"]
                    setting.save()
                    updated_count += 1

        self.stdout.write(
            self.style.SUCCESS(
                f"Configuration initialized successfully! "
                f"Created {created_count} new settings, updated {updated_count} existing settings."
            )
        )
        self.stdout.write(
            self.style.WARNING(
                'You can now manage these settings in the Django Admin under "App Configurations"'
            )
        )
