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):
        # Paperless Integration Settings
        paperless_settings = [
            {
                'key': 'paperless_api_url',
                'display_name': 'Paperless API URL',
                'description': 'The base URL for your Paperless-NGX API (e.g., http://paperless.example.com:8000)',
                'value': 'http://192.168.178.167:30070',
                'default_value': 'http://192.168.178.167:30070',
                'setting_type': 'url',
                'category': 'paperless',
                'order': 1
            },
            {
                'key': 'paperless_api_token',
                'display_name': 'Paperless API Token',
                'description': 'The authentication token for Paperless API access',
                'value': '',
                'default_value': '',
                'setting_type': 'text',
                'category': 'paperless',
                'order': 2
            },
            {
                'key': 'paperless_destinataere_tag',
                'display_name': 'Destinatäre Tag Name',
                'description': 'The tag name used to identify Destinatäre documents in Paperless',
                'value': 'Stiftung_Destinatäre',
                'default_value': 'Stiftung_Destinatäre',
                'setting_type': 'tag',
                'category': 'paperless',
                'order': 3
            },
            {
                'key': 'paperless_destinataere_tag_id',
                'display_name': 'Destinatäre Tag ID',
                'description': 'The numeric ID of the Destinatäre tag in Paperless',
                'value': '210',
                'default_value': '210',
                'setting_type': 'tag_id',
                'category': 'paperless',
                'order': 4
            },
            {
                'key': 'paperless_land_tag',
                'display_name': 'Land & Pächter Tag Name',
                'description': 'The tag name used to identify Land and Pächter documents in Paperless',
                'value': 'Stiftung_Land_und_Pächter',
                'default_value': 'Stiftung_Land_und_Pächter',
                'setting_type': 'tag',
                'category': 'paperless',
                'order': 5
            },
            {
                'key': 'paperless_land_tag_id',
                'display_name': 'Land & Pächter Tag ID',
                'description': 'The numeric ID of the Land & Pächter tag in Paperless',
                'value': '204',
                'default_value': '204',
                'setting_type': 'tag_id',
                'category': 'paperless',
                'order': 6
            },
            {
                'key': 'paperless_admin_tag',
                'display_name': 'Administration Tag Name',
                'description': 'The tag name used to identify Administration documents in Paperless',
                'value': 'Stiftung_Administration',
                'default_value': 'Stiftung_Administration',
                'setting_type': 'tag',
                'category': 'paperless',
                'order': 7
            },
            {
                'key': 'paperless_admin_tag_id',
                'display_name': 'Administration Tag ID',
                'description': 'The numeric ID of the Administration tag in Paperless',
                'value': '216',
                'default_value': '216',
                'setting_type': 'tag_id',
                'category': 'paperless',
                'order': 8
            }
        ]

        created_count = 0
        updated_count = 0

        for setting_data in paperless_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"'
            )
        )
