"""
Management command to initialize corporate identity settings
"""

from django.core.management.base import BaseCommand

from stiftung.models import AppConfiguration


class Command(BaseCommand):
    help = "Initialize corporate identity settings for PDF generation"

    def handle(self, *args, **options):
        corporate_settings = [
            {
                "key": "corporate_stiftung_name",
                "display_name": "Name der Stiftung",
                "description": "Der offizielle Name der Stiftung für PDF-Dokumente",
                "value": "Stiftung",
                "default_value": "Stiftung",
                "setting_type": "text",
                "category": "corporate",
                "order": 1,
            },
            {
                "key": "corporate_logo_path",
                "display_name": "Logo-Pfad",
                "description": "Pfad zur Logo-Datei (relativ zu MEDIA_ROOT oder STATIC_ROOT)",
                "value": "",
                "default_value": "",
                "setting_type": "text",
                "category": "corporate",
                "order": 2,
            },
            {
                "key": "corporate_primary_color",
                "display_name": "Primärfarbe",
                "description": "Hauptfarbe für Überschriften und Akzente (Hex-Code)",
                "value": "#2c3e50",
                "default_value": "#2c3e50",
                "setting_type": "text",
                "category": "corporate",
                "order": 3,
            },
            {
                "key": "corporate_secondary_color",
                "display_name": "Sekundärfarbe",
                "description": "Zweitfarbe für Akzente und Details (Hex-Code)",
                "value": "#3498db",
                "default_value": "#3498db",
                "setting_type": "text",
                "category": "corporate",
                "order": 4,
            },
            {
                "key": "corporate_address_line1",
                "display_name": "Adresse Zeile 1",
                "description": "Erste Zeile der Stiftungsadresse",
                "value": "",
                "default_value": "",
                "setting_type": "text",
                "category": "corporate",
                "order": 5,
            },
            {
                "key": "corporate_address_line2",
                "display_name": "Adresse Zeile 2",
                "description": "Zweite Zeile der Stiftungsadresse (PLZ, Ort)",
                "value": "",
                "default_value": "",
                "setting_type": "text",
                "category": "corporate",
                "order": 6,
            },
            {
                "key": "corporate_phone",
                "display_name": "Telefonnummer",
                "description": "Telefonnummer der Stiftung",
                "value": "",
                "default_value": "",
                "setting_type": "text",
                "category": "corporate",
                "order": 7,
            },
            {
                "key": "corporate_email",
                "display_name": "E-Mail-Adresse",
                "description": "Offizielle E-Mail-Adresse der Stiftung",
                "value": "",
                "default_value": "",
                "setting_type": "text",
                "category": "corporate",
                "order": 8,
            },
            {
                "key": "corporate_website",
                "display_name": "Website",
                "description": "Website der Stiftung",
                "value": "",
                "default_value": "",
                "setting_type": "url",
                "category": "corporate",
                "order": 9,
            },
            {
                "key": "corporate_footer_text",
                "display_name": "Fußzeilen-Text",
                "description": "Text für die Fußzeile in PDF-Dokumenten",
                "value": "Dieser Bericht wurde automatisch generiert.",
                "default_value": "Dieser Bericht wurde automatisch generiert.",
                "setting_type": "text",
                "category": "corporate",
                "order": 10,
            },
        ]

        created_count = 0
        updated_count = 0

        for setting_data in corporate_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"Corporate identity settings initialized! "
                f"Created {created_count} new settings, updated {updated_count} existing settings."
            )
        )

        if created_count > 0:
            self.stdout.write(
                self.style.WARNING(
                    "Please configure your corporate identity settings in "
                    "Administration -> Application Settings before generating PDFs."
                )
            )
