from django.core.management.base import BaseCommand
from apps.business.models import Business
from apps.warranty.models import WarrantyTemplate


class Command(BaseCommand):
    help = 'Initialize default warranty templates for all businesses'
    
    def add_arguments(self, parser):
        parser.add_argument(
            '--business-id',
            type=int,
            help='Initialize templates for specific business only'
        )
    
    def handle(self, *args, **options):
        business_id = options.get('business_id')
        
        if business_id:
            businesses = Business.objects.filter(id=business_id)
        else:
            businesses = Business.objects.filter(is_active=True)
        
        templates_data = [
            {
                'name': 'Laptop Hardware Diagnosis',
                'category': 'Laptop',
                'common_issues': [
                    'Screen not displaying',
                    'Keyboard not working',
                    'Touchpad issues',
                    'Battery not charging',
                    'Overheating',
                    'Won\'t power on'
                ],
                'standard_procedures': '''
1. Visual inspection for physical damage
2. Power on test
3. Display functionality check
4. Keyboard and touchpad test
5. Port functionality verification
6. Battery health assessment
7. Temperature monitoring
8. Hardware diagnostic software run
                ''',
                'estimated_time_hours': 2.0
            },
            {
                'name': 'Desktop Hardware Diagnosis',
                'category': 'Desktop',
                'common_issues': [
                    'Won\'t boot',
                    'Blue screen errors',
                    'Slow performance',
                    'No display output',
                    'USB ports not working',
                    'Overheating'
                ],
                'standard_procedures': '''
1. Power supply test
2. RAM diagnostic
3. Hard drive health check
4. Graphics card test
5. Motherboard inspection
6. Cable connection verification
7. BIOS/UEFI settings check
8. Component stress testing
                ''',
                'estimated_time_hours': 1.5
            },
            {
                'name': 'Software Troubleshooting',
                'category': 'Software',
                'common_issues': [
                    'Operating system crashes',
                    'Application errors',
                    'Virus/malware infection',
                    'Slow performance',
                    'Driver issues',
                    'Network connectivity problems'
                ],
                'standard_procedures': '''
1. System diagnostic scan
2. Antivirus/anti-malware scan
3. Driver update verification
4. System file integrity check
5. Registry cleanup
6. Startup program optimization
7. Network configuration check
8. System restore if necessary
                ''',
                'estimated_time_hours': 3.0
            },
            {
                'name': 'Monitor Repair',
                'category': 'Monitor',
                'common_issues': [
                    'No display',
                    'Flickering screen',
                    'Dead pixels',
                    'Color distortion',
                    'Backlight issues',
                    'Input connection problems'
                ],
                'standard_procedures': '''
1. Power cable verification
2. Input cable testing
3. OSD menu functionality check
4. Backlight inspection
5. Panel condition assessment
6. Color calibration test
7. Pixel mapping
8. Connection port examination
                ''',
                'estimated_time_hours': 1.0
            }
        ]
        
        created_count = 0
        
        for business in businesses:
            for template_data in templates_data:
                template, created = WarrantyTemplate.objects.get_or_create(
                    business=business,
                    name=template_data['name'],
                    defaults=template_data
                )
                
                if created:
                    created_count += 1
                    self.stdout.write(
                        self.style.SUCCESS(
                            f'Created template "{template.name}" for {business.name}'
                        )
                    )
        
        self.stdout.write(
            self.style.SUCCESS(
                f'Successfully created {created_count} warranty templates'
            )
        )

