"""
Management command to seed default observation categories.

Usage:
    python manage.py seed_observation_categories
    python manage.py seed_observation_categories --clear  # Clear existing and reseed
"""
from django.core.management.base import BaseCommand

from apps.observations.models import ObservationCategory


class Command(BaseCommand):
    help = 'Seed default observation categories with bilingual names'

    def add_arguments(self, parser):
        parser.add_argument(
            '--clear',
            action='store_true',
            help='Clear existing categories before seeding',
        )

    def handle(self, *args, **options):
        if options['clear']:
            self.stdout.write('Clearing existing categories...')
            ObservationCategory.objects.all().delete()

        # Default categories with bilingual names
        categories = [
            {
                'name_en': 'Patient Safety',
                'name_ar': 'سلامة المرضى',
                'description': 'Issues related to patient safety and potential harm',
                'icon': 'bi-shield-exclamation',
                'sort_order': 1,
            },
            {
                'name_en': 'Clinical Quality',
                'name_ar': 'الجودة السريرية',
                'description': 'Clinical care quality concerns',
                'icon': 'bi-heart-pulse',
                'sort_order': 2,
            },
            {
                'name_en': 'Infection Control',
                'name_ar': 'مكافحة العدوى',
                'description': 'Infection prevention and control issues',
                'icon': 'bi-virus',
                'sort_order': 3,
            },
            {
                'name_en': 'Medication Safety',
                'name_ar': 'سلامة الأدوية',
                'description': 'Medication errors or near misses',
                'icon': 'bi-capsule',
                'sort_order': 4,
            },
            {
                'name_en': 'Equipment & Devices',
                'name_ar': 'المعدات والأجهزة',
                'description': 'Medical equipment or device issues',
                'icon': 'bi-tools',
                'sort_order': 5,
            },
            {
                'name_en': 'Facility & Environment',
                'name_ar': 'المرافق والبيئة',
                'description': 'Building, maintenance, or environmental concerns',
                'icon': 'bi-building',
                'sort_order': 6,
            },
            {
                'name_en': 'Staff Behavior',
                'name_ar': 'سلوك الموظفين',
                'description': 'Staff conduct or professionalism concerns',
                'icon': 'bi-people',
                'sort_order': 7,
            },
            {
                'name_en': 'Communication',
                'name_ar': 'التواصل',
                'description': 'Communication breakdowns or issues',
                'icon': 'bi-chat-dots',
                'sort_order': 8,
            },
            {
                'name_en': 'Documentation',
                'name_ar': 'التوثيق',
                'description': 'Medical records or documentation issues',
                'icon': 'bi-file-text',
                'sort_order': 9,
            },
            {
                'name_en': 'Process & Workflow',
                'name_ar': 'العمليات وسير العمل',
                'description': 'Process inefficiencies or workflow problems',
                'icon': 'bi-diagram-3',
                'sort_order': 10,
            },
            {
                'name_en': 'Security',
                'name_ar': 'الأمن',
                'description': 'Security concerns or incidents',
                'icon': 'bi-shield-lock',
                'sort_order': 11,
            },
            {
                'name_en': 'IT & Systems',
                'name_ar': 'تقنية المعلومات والأنظمة',
                'description': 'IT systems or technology issues',
                'icon': 'bi-pc-display',
                'sort_order': 12,
            },
            {
                'name_en': 'Housekeeping',
                'name_ar': 'التدبير المنزلي',
                'description': 'Cleanliness and housekeeping issues',
                'icon': 'bi-house',
                'sort_order': 13,
            },
            {
                'name_en': 'Food Services',
                'name_ar': 'خدمات الطعام',
                'description': 'Food quality or dietary concerns',
                'icon': 'bi-cup-hot',
                'sort_order': 14,
            },
            {
                'name_en': 'Other',
                'name_ar': 'أخرى',
                'description': 'Other observations not fitting other categories',
                'icon': 'bi-three-dots',
                'sort_order': 99,
            },
        ]

        created_count = 0
        updated_count = 0

        for cat_data in categories:
            category, created = ObservationCategory.objects.update_or_create(
                name_en=cat_data['name_en'],
                defaults={
                    'name_ar': cat_data['name_ar'],
                    'description': cat_data['description'],
                    'icon': cat_data['icon'],
                    'sort_order': cat_data['sort_order'],
                    'is_active': True,
                }
            )
            if created:
                created_count += 1
                self.stdout.write(f'  Created: {category.name_en}')
            else:
                updated_count += 1
                self.stdout.write(f'  Updated: {category.name_en}')

        self.stdout.write(
            self.style.SUCCESS(
                f'\nSuccessfully seeded observation categories: '
                f'{created_count} created, {updated_count} updated'
            )
        )
