from django.core.management.base import BaseCommand
from django.db import transaction
from services.models import Service, ServiceCategory, ServiceFeature


class Command(BaseCommand):
    help = 'Populate the database with sample services'

    def handle(self, *args, **options):
        with transaction.atomic():
            # Create categories
            web_dev_category, created = ServiceCategory.objects.get_or_create(
                slug='web-development',
                defaults={
                    'name': 'Web Development',
                    'description': 'Custom web applications and websites',
                    'display_order': 1,
                    'is_active': True
                }
            )
            
            mobile_category, created = ServiceCategory.objects.get_or_create(
                slug='mobile-development',
                defaults={
                    'name': 'Mobile Development',
                    'description': 'Native and cross-platform mobile apps',
                    'display_order': 2,
                    'is_active': True
                }
            )
            
            api_category, created = ServiceCategory.objects.get_or_create(
                slug='api-development',
                defaults={
                    'name': 'API Development',
                    'description': 'RESTful APIs and microservices',
                    'display_order': 3,
                    'is_active': True
                }
            )

            # Create services
            services_data = [
                {
                    'title': 'Custom Web Application Development',
                    'description': 'Build powerful, scalable web applications tailored to your business needs. Our expert developers use modern frameworks and technologies to create solutions that drive growth and efficiency.',
                    'short_description': 'Custom web applications built with modern technologies for optimal performance and scalability.',
                    'slug': 'custom-web-application-development',
                    'icon': 'code',
                    'price': '5000.00',
                    'category': web_dev_category,
                    'duration': '4-8 weeks',
                    'deliverables': 'Responsive Web Application, Admin Dashboard, User Authentication, Database Design, API Integration, Testing & Deployment',
                    'technologies': 'React, Next.js, Node.js, PostgreSQL, AWS, Docker',
                    'process_steps': 'Requirements Analysis, System Design, Development, Testing, Deployment, Training',
                    'featured': True,
                    'display_order': 1,
                    'is_active': True
                },
                {
                    'title': 'Mobile App Development',
                    'description': 'Create stunning mobile applications for iOS and Android platforms. We deliver native and cross-platform solutions that provide exceptional user experiences and drive engagement.',
                    'short_description': 'Native and cross-platform mobile applications for iOS and Android.',
                    'slug': 'mobile-app-development',
                    'icon': 'mobile',
                    'price': '8000.00',
                    'category': mobile_category,
                    'duration': '6-12 weeks',
                    'deliverables': 'Native Mobile App, Cross-platform App, App Store Deployment, Push Notifications, Offline Support, Analytics Integration',
                    'technologies': 'React Native, Flutter, Swift, Kotlin, Firebase, App Store Connect',
                    'process_steps': 'UI/UX Design, Development, Testing, App Store Submission, Launch Support',
                    'featured': True,
                    'display_order': 2,
                    'is_active': True
                },
                {
                    'title': 'API Development & Integration',
                    'description': 'Build robust, scalable APIs that power your applications and enable seamless integration with third-party services. Our APIs are designed for performance, security, and maintainability.',
                    'short_description': 'RESTful APIs and microservices for seamless system integration.',
                    'slug': 'api-development-integration',
                    'icon': 'api',
                    'price': '3000.00',
                    'category': api_category,
                    'duration': '2-4 weeks',
                    'deliverables': 'RESTful API, API Documentation, Authentication System, Rate Limiting, Monitoring, Integration Support',
                    'technologies': 'Node.js, Python, FastAPI, PostgreSQL, Redis, AWS API Gateway',
                    'process_steps': 'API Planning, Development, Documentation, Testing, Deployment, Integration',
                    'featured': False,
                    'display_order': 3,
                    'is_active': True
                },
                {
                    'title': 'Cloud Migration Services',
                    'description': 'Migrate your existing infrastructure to the cloud with minimal downtime. We help you leverage cloud technologies for improved scalability, security, and cost efficiency.',
                    'short_description': 'Seamless migration to cloud platforms with enhanced security and scalability.',
                    'slug': 'cloud-migration-services',
                    'icon': 'cloud',
                    'price': '10000.00',
                    'category': web_dev_category,
                    'duration': '8-16 weeks',
                    'deliverables': 'Cloud Infrastructure Setup, Data Migration, Security Configuration, Monitoring Setup, Training, Ongoing Support',
                    'technologies': 'AWS, Azure, Google Cloud, Docker, Kubernetes, Terraform',
                    'process_steps': 'Assessment, Migration Planning, Implementation, Testing, Optimization, Support',
                    'featured': True,
                    'display_order': 4,
                    'is_active': True
                }
            ]

            for service_data in services_data:
                service, created = Service.objects.get_or_create(
                    slug=service_data['slug'],
                    defaults=service_data
                )
                
                if created:
                    self.stdout.write(
                        self.style.SUCCESS(f'Created service: {service.title}')
                    )
                    
                    # Add features for each service
                    if service.slug == 'custom-web-application-development':
                        features = [
                            {
                                'title': 'Responsive Design',
                                'description': 'Mobile-first design that works perfectly on all devices',
                                'icon': 'mobile',
                                'display_order': 1
                            },
                            {
                                'title': 'Performance Optimization',
                                'description': 'Fast loading times and optimized user experience',
                                'icon': 'bolt',
                                'display_order': 2
                            },
                            {
                                'title': 'Security Features',
                                'description': 'Built-in security measures to protect your data',
                                'icon': 'shield',
                                'display_order': 3
                            }
                        ]
                    elif service.slug == 'mobile-app-development':
                        features = [
                            {
                                'title': 'Cross-Platform',
                                'description': 'Single codebase for both iOS and Android',
                                'icon': 'mobile',
                                'display_order': 1
                            },
                            {
                                'title': 'Native Performance',
                                'description': 'Optimized performance using native components',
                                'icon': 'gauge',
                                'display_order': 2
                            },
                            {
                                'title': 'App Store Ready',
                                'description': 'Complete app store submission and approval process',
                                'icon': 'store',
                                'display_order': 3
                            }
                        ]
                    elif service.slug == 'api-development-integration':
                        features = [
                            {
                                'title': 'RESTful Design',
                                'description': 'Clean, intuitive API design following REST principles',
                                'icon': 'code',
                                'display_order': 1
                            },
                            {
                                'title': 'Comprehensive Documentation',
                                'description': 'Detailed API documentation with examples',
                                'icon': 'book',
                                'display_order': 2
                            },
                            {
                                'title': 'Rate Limiting',
                                'description': 'Built-in rate limiting and security measures',
                                'icon': 'shield',
                                'display_order': 3
                            }
                        ]
                    elif service.slug == 'cloud-migration-services':
                        features = [
                            {
                                'title': 'Zero Downtime',
                                'description': 'Seamless migration with minimal service interruption',
                                'icon': 'clock',
                                'display_order': 1
                            },
                            {
                                'title': 'Cost Optimization',
                                'description': 'Optimized cloud resources for maximum cost efficiency',
                                'icon': 'dollar-sign',
                                'display_order': 2
                            },
                            {
                                'title': 'Security First',
                                'description': 'Enhanced security with cloud-native security features',
                                'icon': 'shield',
                                'display_order': 3
                            }
                        ]
                    else:
                        features = []
                    
                    for feature_data in features:
                        ServiceFeature.objects.create(
                            service=service,
                            **feature_data
                        )
                else:
                    self.stdout.write(
                        self.style.WARNING(f'Service already exists: {service.title}')
                    )

        self.stdout.write(
            self.style.SUCCESS('Successfully populated services database')
        )