"""
Management command to import enterprise sample data for services
with images downloaded from Unsplash.
"""
import urllib.request
import urllib.parse
import json
from io import BytesIO
from django.core.management.base import BaseCommand
from django.core.files.base import ContentFile
from django.db import transaction
from services.models import Service, ServiceCategory, ServiceFeature, ServiceExpertise


class Command(BaseCommand):
    help = 'Import enterprise sample data for services with images from Unsplash'

    def add_arguments(self, parser):
        parser.add_argument(
            '--unsplash-key',
            type=str,
            help='Unsplash API access key (optional, uses Unsplash Source API if not provided)',
        )
        parser.add_argument(
            '--width',
            type=int,
            default=1200,
            help='Image width in pixels (default: 1200)',
        )
        parser.add_argument(
            '--height',
            type=int,
            default=800,
            help='Image height in pixels (default: 800)',
        )
        parser.add_argument(
            '--update-existing',
            action='store_true',
            help='Update existing services instead of skipping them',
        )

    def download_image_from_unsplash(self, keyword, width=1200, height=800, api_key=None, photo_id=None):
        """
        Download an image from Unsplash based on keyword or photo ID.
        
        Args:
            keyword: Search keyword for the image
            width: Image width in pixels
            height: Image height in pixels
            api_key: Optional Unsplash API access key
            photo_id: Optional specific Unsplash photo ID
        
        Returns:
            BytesIO object containing the image data, or None if download fails
        """
        try:
            if api_key:
                # Use Unsplash API (requires API key)
                if photo_id:
                    # Get specific photo by ID
                    url = f"https://api.unsplash.com/photos/{photo_id}"
                    req = urllib.request.Request(url)
                    req.add_header('Authorization', f'Client-ID {api_key}')
                    with urllib.request.urlopen(req, timeout=30) as response:
                        data = json.loads(response.read().decode())
                        image_url = data['urls']['regular']
                else:
                    # Get random photo by keyword
                    url = "https://api.unsplash.com/photos/random"
                    params = {
                        'query': keyword,
                        'orientation': 'landscape',
                        'w': width,
                        'h': height,
                        'client_id': api_key
                    }
                    query_string = urllib.parse.urlencode(params)
                    full_url = f"{url}?{query_string}"
                    
                    req = urllib.request.Request(full_url)
                    with urllib.request.urlopen(req, timeout=30) as response:
                        data = json.loads(response.read().decode())
                        image_url = data['urls']['regular']
            else:
                # Use Picsum Photos (Lorem Picsum) for reliable, unique images (no key required)
                # Create a unique seed based on keyword hash to get different images for each service
                keyword_hash = abs(hash(keyword)) % 1000
                # Picsum Photos provides random images with seeds - each seed gives a unique image
                image_url = f"https://picsum.photos/seed/{keyword_hash}/{width}/{height}"
            
            # Download the actual image
            req = urllib.request.Request(image_url)
            req.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36')
            with urllib.request.urlopen(req, timeout=30) as img_response:
                image_data = BytesIO(img_response.read())
                image_data.seek(0)
            
            return image_data
        except Exception as e:
            self.stdout.write(
                self.style.WARNING(f'Failed to download image for "{keyword}": {str(e)}')
            )
            return None

    def handle(self, *args, **options):
        unsplash_key = options.get('unsplash_key')
        width = options.get('width', 1200)
        height = options.get('height', 800)
        update_existing = options.get('update_existing', False)

        self.stdout.write(self.style.SUCCESS('Starting enterprise data import...'))

        with transaction.atomic():
            # Delete all existing services (this will cascade delete features and expertise items)
            deleted_count = Service.objects.count()
            Service.objects.all().delete()
            self.stdout.write(
                self.style.WARNING(f'Deleted {deleted_count} existing service(s)')
            )

            # Create or get categories
            categories = {}
            category_data = [
                {
                    'slug': 'development',
                    'name': 'Development',
                    'description': 'Software development services',
                    'display_order': 1,
                },
                {
                    'slug': 'infrastructure',
                    'name': 'Infrastructure',
                    'description': 'Infrastructure and DevOps services',
                    'display_order': 2,
                },
                {
                    'slug': 'integration',
                    'name': 'Integration',
                    'description': 'API and data integration services',
                    'display_order': 3,
                },
                {
                    'slug': 'ai-services',
                    'name': 'AI Services',
                    'description': 'Artificial Intelligence and machine learning services',
                    'display_order': 4,
                },
            ]

            for cat_data in category_data:
                category, created = ServiceCategory.objects.get_or_create(
                    slug=cat_data['slug'],
                    defaults=cat_data
                )
                categories[cat_data['slug']] = category
                if created:
                    self.stdout.write(
                        self.style.SUCCESS(f'Created category: {category.name}')
                    )

            # New services data
            services_data = [
                {
                    'title': 'Enterprise Backend Development Services',
                    'description': 'Expert backend engineering services to build robust, scalable server-side applications. We design and develop high-performance APIs, microservices, and database architectures that power your applications. Our backend solutions ensure reliability, security, and optimal performance for enterprise-scale systems.',
                    'short_description': 'Robust backend engineering services for scalable server-side applications and APIs.',
                    'slug': 'enterprise-backend-development-services',
                    'icon': 'server',
                    'price': '25000.00',
                    'category_slug': 'development',
                    'duration': '8-16 weeks',
                    'deliverables': 'Backend API Development, Database Design & Optimization, Microservices Architecture, Authentication & Authorization, API Documentation, Testing Suite, Deployment Configuration, Performance Optimization, Security Implementation, Documentation',
                    'technologies': 'Node.js, Python, Django, FastAPI, PostgreSQL, MongoDB, Redis, GraphQL, REST APIs, Docker, Kubernetes, AWS, Microservices',
                    'process_steps': 'Requirements Analysis, Architecture Design, Database Design, API Development, Testing & QA, Security Audit, Deployment, Documentation, Training, Ongoing Support',
                    'features_description': 'Enterprise-grade backend solutions with scalable architecture, secure authentication, optimized database performance, and comprehensive API documentation.',
                    'deliverables_description': 'Complete backend infrastructure with APIs, databases, authentication systems, and comprehensive documentation for seamless integration.',
                    'process_description': 'We follow agile development practices with iterative development, continuous testing, and regular code reviews to ensure quality.',
                    'why_choose_description': 'Our backend engineering expertise ensures your applications are built on solid foundations with scalability, security, and performance in mind.',
                    'expertise_description': 'Experienced backend engineers with deep knowledge of modern frameworks, database optimization, and cloud-native architectures.',
                    'image_keyword': 'server backend code programming database',
                    'unsplash_photo_id': '1558497419-80c43fea0fbe',  # Backend/server technology
                    'featured': True,
                    'display_order': 1,
                    'is_active': True,
                },
                {
                    'title': 'Enterprise Frontend Development Services',
                    'description': 'Create stunning, responsive user interfaces with our frontend engineering services. We build modern, performant web applications using the latest frameworks and technologies. Our frontend solutions deliver exceptional user experiences across all devices and platforms.',
                    'short_description': 'Modern frontend engineering services for responsive and performant web applications.',
                    'slug': 'enterprise-frontend-development-services',
                    'icon': 'code',
                    'price': '22000.00',
                    'category_slug': 'development',
                    'duration': '8-16 weeks',
                    'deliverables': 'Responsive Web Application, Component Library, State Management, API Integration, Performance Optimization, Cross-browser Compatibility, Accessibility Implementation, Testing Suite, Build Configuration, Documentation',
                    'technologies': 'React, Next.js, Vue.js, TypeScript, JavaScript, HTML5, CSS3, Tailwind CSS, Redux, GraphQL, Webpack, Vite',
                    'process_steps': 'UI/UX Design Review, Component Architecture, Development, State Management, API Integration, Testing, Performance Optimization, Browser Testing, Deployment, Documentation',
                    'features_description': 'Modern frontend applications with responsive design, optimized performance, accessibility compliance, and seamless user experiences.',
                    'deliverables_description': 'Complete frontend application with reusable components, optimized performance, and comprehensive documentation for maintenance.',
                    'process_description': 'We use modern development practices with component-based architecture, automated testing, and continuous integration.',
                    'why_choose_description': 'Our frontend engineering expertise ensures your applications are fast, accessible, and provide exceptional user experiences.',
                    'expertise_description': 'Skilled frontend developers with expertise in modern frameworks, performance optimization, and user experience design.',
                    'image_keyword': 'web design user interface frontend development',
                    'unsplash_photo_id': '1460925895917-afdab827c52f',  # Web design/UI
                    'featured': True,
                    'display_order': 2,
                    'is_active': True,
                },
                {
                    'title': 'Enterprise Data Replication & Synchronization Services',
                    'description': 'Comprehensive data replication services to ensure data consistency, availability, and disaster recovery. We implement robust data replication strategies across multiple systems and locations, ensuring your data is always available and synchronized.',
                    'short_description': 'Reliable data replication services for data consistency and disaster recovery.',
                    'slug': 'enterprise-data-replication-synchronization-services',
                    'icon': 'database',
                    'price': '18000.00',
                    'category_slug': 'infrastructure',
                    'duration': '4-8 weeks',
                    'deliverables': 'Data Replication Strategy, Replication Setup, Monitoring & Alerting, Disaster Recovery Plan, Data Synchronization, Backup Systems, Documentation, Training, Ongoing Support',
                    'technologies': 'PostgreSQL, MySQL, MongoDB, Redis, AWS RDS, Azure SQL, Data Sync Tools, ETL Pipelines, Change Data Capture',
                    'process_steps': 'Data Assessment, Replication Strategy, Setup & Configuration, Testing, Monitoring Setup, Disaster Recovery Planning, Documentation, Training, Go-Live, Ongoing Support',
                    'features_description': 'Robust data replication with real-time synchronization, automated failover, monitoring, and comprehensive disaster recovery capabilities.',
                    'deliverables_description': 'Complete data replication solution with monitoring, backup systems, and disaster recovery plans for business continuity.',
                    'process_description': 'We implement data replication with careful planning, thorough testing, and continuous monitoring to ensure reliability.',
                    'why_choose_description': 'Our data replication expertise ensures your data is always available, synchronized, and protected against data loss.',
                    'expertise_description': 'Data engineers with extensive experience in database replication, data synchronization, and disaster recovery solutions.',
                    'image_keyword': 'database data center server backup storage',
                    'unsplash_photo_id': '1557804506-2a27b1fb0947',  # Database/data center
                    'featured': False,
                    'display_order': 3,
                    'is_active': True,
                },
                {
                    'title': 'IT Infrastructure Management & Support Services',
                    'description': 'Comprehensive infrastructure support services to maintain, monitor, and optimize your IT infrastructure. We provide 24/7 support, proactive monitoring, and infrastructure management to ensure your systems run smoothly and efficiently.',
                    'short_description': 'Complete infrastructure support services for reliable and optimized IT systems.',
                    'slug': 'it-infrastructure-management-support-services',
                    'icon': 'settings',
                    'price': '30000.00',
                    'category_slug': 'infrastructure',
                    'duration': 'Ongoing',
                    'deliverables': '24/7 Monitoring, Incident Response, Performance Optimization, Security Updates, Backup Management, Capacity Planning, Documentation, Regular Reports, Proactive Maintenance, Support',
                    'technologies': 'AWS, Azure, GCP, Docker, Kubernetes, Terraform, Ansible, Monitoring Tools, Logging Systems, CI/CD',
                    'process_steps': 'Infrastructure Assessment, Monitoring Setup, Support Process, Incident Response Plan, Regular Maintenance, Performance Optimization, Security Updates, Reporting, Continuous Improvement',
                    'features_description': 'Comprehensive infrastructure support with 24/7 monitoring, proactive maintenance, rapid incident response, and continuous optimization.',
                    'deliverables_description': 'Complete infrastructure support package with monitoring, maintenance, and optimization services for reliable operations.',
                    'process_description': 'We provide ongoing support with proactive monitoring, regular maintenance, and continuous optimization of your infrastructure.',
                    'why_choose_description': 'Our infrastructure support ensures your systems are always running optimally with minimal downtime and maximum reliability.',
                    'expertise_description': 'Infrastructure specialists with expertise in cloud platforms, containerization, and infrastructure automation.',
                    'image_keyword': 'cloud infrastructure network technology data center',
                    'unsplash_photo_id': '1557804506-2a27b1fb0947',  # Cloud infrastructure
                    'featured': True,
                    'display_order': 4,
                    'is_active': True,
                },
                {
                    'title': 'Enterprise Server Management & Administration Services',
                    'description': 'Complete server management and configuration services for optimal performance and reliability. We handle server setup, configuration, optimization, and maintenance to ensure your servers run efficiently and securely.',
                    'short_description': 'Professional server management and configuration services for optimal performance.',
                    'slug': 'enterprise-server-management-administration-services',
                    'icon': 'server',
                    'price': '20000.00',
                    'category_slug': 'infrastructure',
                    'duration': 'Ongoing',
                    'deliverables': 'Server Setup & Configuration, Performance Optimization, Security Hardening, Monitoring Setup, Backup Configuration, Maintenance Plans, Documentation, Training, Support',
                    'technologies': 'Linux, Windows Server, Nginx, Apache, Docker, Kubernetes, Cloud Servers, Monitoring Tools, Backup Solutions',
                    'process_steps': 'Server Assessment, Setup & Configuration, Security Hardening, Performance Tuning, Monitoring Setup, Backup Configuration, Documentation, Training, Ongoing Maintenance',
                    'features_description': 'Comprehensive server services with optimized configuration, security hardening, performance tuning, and proactive monitoring.',
                    'deliverables_description': 'Fully configured and optimized servers with monitoring, backups, and maintenance plans for reliable operations.',
                    'process_description': 'We provide complete server management with careful configuration, security hardening, and ongoing optimization.',
                    'why_choose_description': 'Our server services ensure your servers are configured optimally, secure, and running at peak performance.',
                    'expertise_description': 'Server administrators with deep knowledge of server configuration, optimization, and security best practices.',
                    'image_keyword': 'server rack data center hardware technology',
                    'unsplash_photo_id': '1558497419-80c43fea0fbe',  # Server rack
                    'featured': False,
                    'display_order': 5,
                    'is_active': True,
                },
                {
                    'title': 'DevOps Engineering & Infrastructure Automation Services',
                    'description': 'Comprehensive DevOps services to streamline your software delivery pipeline. We implement CI/CD pipelines, infrastructure automation, and DevOps best practices to accelerate development and improve deployment reliability.',
                    'short_description': 'Complete DevOps services for automated software delivery and infrastructure management.',
                    'slug': 'devops-engineering-infrastructure-automation-services',
                    'icon': 'settings',
                    'price': '28000.00',
                    'category_slug': 'infrastructure',
                    'duration': '8-16 weeks',
                    'deliverables': 'CI/CD Pipeline, Infrastructure as Code, Automated Testing, Deployment Automation, Monitoring & Logging, Security Scanning, Documentation, Team Training, Best Practices, Support',
                    'technologies': 'Jenkins, GitLab CI, GitHub Actions, Docker, Kubernetes, Terraform, Ansible, Prometheus, Grafana, ELK Stack',
                    'process_steps': 'DevOps Assessment, Tool Selection, Pipeline Design, Implementation, Testing, Documentation, Training, Go-Live, Optimization, Ongoing Support',
                    'features_description': 'Complete DevOps solutions with automated pipelines, infrastructure automation, comprehensive monitoring, and security integration.',
                    'deliverables_description': 'Full DevOps infrastructure with automated pipelines, monitoring dashboards, and team training for sustainable operations.',
                    'process_description': 'We implement DevOps best practices with automation, quality gates, and continuous improvement.',
                    'why_choose_description': 'Our DevOps services help you achieve faster deployments, improved quality, and reduced operational overhead.',
                    'expertise_description': 'DevOps engineers with expertise in CI/CD tools, containerization, and infrastructure automation.',
                    'image_keyword': 'devops automation deployment pipeline technology',
                    'unsplash_photo_id': '1558497419-80c43fea0fbe',  # DevOps/automation
                    'featured': True,
                    'display_order': 6,
                    'is_active': True,
                },
                {
                    'title': 'Enterprise API Integration Services',
                    'description': 'Seamless API integration services to connect your applications with third-party services and systems. We handle complex integrations, data transformation, and API management to enable smooth communication between different systems.',
                    'short_description': 'Professional API integration services for seamless system connectivity.',
                    'slug': 'enterprise-api-integration-services',
                    'icon': 'api',
                    'price': '15000.00',
                    'category_slug': 'integration',
                    'duration': '4-12 weeks',
                    'deliverables': 'API Integration Setup, Data Transformation, Error Handling, Authentication Configuration, Testing Suite, Documentation, Monitoring, Support',
                    'technologies': 'REST APIs, GraphQL, Webhooks, OAuth 2.0, JWT, Postman, API Gateway, Middleware, ETL Tools',
                    'process_steps': 'API Analysis, Integration Design, Development, Authentication Setup, Testing, Error Handling, Documentation, Deployment, Monitoring, Support',
                    'features_description': 'Comprehensive API integrations with secure authentication, error handling, data transformation, and monitoring capabilities.',
                    'deliverables_description': 'Complete API integration solution with documentation, testing, and monitoring for reliable connectivity.',
                    'process_description': 'We implement API integrations with careful design, thorough testing, and comprehensive error handling.',
                    'why_choose_description': 'Our API integration expertise ensures seamless connectivity between your systems and third-party services.',
                    'expertise_description': 'Integration specialists with experience in various APIs, authentication methods, and data transformation.',
                    'image_keyword': 'api integration network connection technology',
                    'unsplash_photo_id': '1460925895917-afdab827c52f',  # API/network
                    'featured': False,
                    'display_order': 7,
                    'is_active': True,
                },
                {
                    'title': 'Artificial Intelligence & Machine Learning Solutions',
                    'description': 'Cutting-edge AI and machine learning services to transform your business with intelligent automation and insights. We develop custom AI solutions, implement machine learning models, and integrate AI capabilities into your applications.',
                    'short_description': 'Advanced AI and machine learning services for intelligent business solutions.',
                    'slug': 'artificial-intelligence-machine-learning-solutions',
                    'icon': 'brain',
                    'price': '40000.00',
                    'category_slug': 'ai-services',
                    'duration': '12-24 weeks',
                    'deliverables': 'AI Strategy, Machine Learning Models, Model Training & Deployment, AI Integration, Data Pipeline, Model Monitoring, Documentation, Training, Support',
                    'technologies': 'Python, TensorFlow, PyTorch, Scikit-learn, OpenAI API, LangChain, MLflow, AWS SageMaker, Azure ML, Google AI',
                    'process_steps': 'AI Strategy, Data Preparation, Model Development, Training & Validation, Deployment, Integration, Monitoring, Optimization, Documentation, Training',
                    'features_description': 'Advanced AI solutions with custom machine learning models, intelligent automation, and seamless integration into your applications.',
                    'deliverables_description': 'Complete AI solution with trained models, deployment infrastructure, and monitoring systems for continuous improvement.',
                    'process_description': 'We develop AI solutions with iterative model development, thorough validation, and continuous monitoring for optimal performance.',
                    'why_choose_description': 'Our AI expertise helps you leverage cutting-edge technology to automate processes and gain valuable insights from your data.',
                    'expertise_description': 'AI specialists with deep knowledge of machine learning, deep learning, and AI integration into business applications.',
                    'image_keyword': 'artificial intelligence machine learning neural network',
                    'unsplash_photo_id': '1558497419-80c43fea0fbe',  # AI/ML technology
                    'featured': True,
                    'display_order': 8,
                    'is_active': True,
                },
            ]

            # Process each service
            for service_data in services_data:
                category_slug = service_data.pop('category_slug')
                image_keyword = service_data.pop('image_keyword')
                photo_id = service_data.pop('unsplash_photo_id', None)
                category = categories.get(category_slug)
                
                if not category:
                    self.stdout.write(
                        self.style.ERROR(f'Category "{category_slug}" not found for service: {service_data["title"]}')
                    )
                    continue

                service_data['category'] = category

                # Create service (we already deleted all, so this will always create new)
                service = Service.objects.create(**service_data)
                self.stdout.write(
                    self.style.SUCCESS(f'Created service: {service.title}')
                )

                # Download and set image
                if service:
                    self.stdout.write(f'Downloading image for: {service.title}...')
                    image_data = self.download_image_from_unsplash(
                        image_keyword,
                        width=width,
                        height=height,
                        api_key=unsplash_key,
                        photo_id=photo_id
                    )

                    if image_data:
                        try:
                            # Determine file extension (default to jpg)
                            filename = f"{service.slug}.jpg"
                            
                            # Save image to service
                            service.image.save(
                                filename,
                                ContentFile(image_data.read()),
                                save=True
                            )
                            self.stdout.write(
                                self.style.SUCCESS(f'  ✓ Image saved for: {service.title}')
                            )
                        except Exception as e:
                            self.stdout.write(
                                self.style.ERROR(f'  ✗ Failed to save image: {str(e)}')
                            )
                    else:
                        self.stdout.write(
                            self.style.WARNING(f'  ⚠ No image downloaded for: {service.title}')
                        )

                    # Add features for services
                    features_data = [
                        {
                            'title': 'Enterprise-Grade Security',
                            'description': 'Bank-level security with encryption, authentication, and compliance standards',
                            'icon': 'shield',
                            'display_order': 1
                        },
                        {
                            'title': 'Scalable Architecture',
                            'description': 'Built to handle growth with horizontal scaling and load balancing',
                            'icon': 'trending-up',
                            'display_order': 2
                        },
                        {
                            'title': '24/7 Support',
                            'description': 'Round-the-clock support and monitoring for mission-critical systems',
                            'icon': 'headphones',
                            'display_order': 3
                        },
                        {
                            'title': 'Comprehensive Documentation',
                            'description': 'Detailed documentation for developers, administrators, and end users',
                            'icon': 'book',
                            'display_order': 4
                        },
                    ]

                    for feature_data in features_data:
                        ServiceFeature.objects.get_or_create(
                            service=service,
                            title=feature_data['title'],
                            defaults=feature_data
                        )

                    # Add expertise items
                    expertise_data = [
                        {
                            'title': 'Industry Expertise',
                            'description': 'Deep understanding of enterprise requirements and industry best practices',
                            'icon': 'award',
                            'display_order': 1
                        },
                        {
                            'title': 'Proven Methodology',
                            'description': 'Agile development with proven processes and quality assurance',
                            'icon': 'check-circle',
                            'display_order': 2
                        },
                        {
                            'title': 'Technology Leadership',
                            'description': 'Cutting-edge technologies and modern development practices',
                            'icon': 'code',
                            'display_order': 3
                        },
                    ]

                    for expertise_data_item in expertise_data:
                        ServiceExpertise.objects.get_or_create(
                            service=service,
                            title=expertise_data_item['title'],
                            defaults=expertise_data_item
                        )

        self.stdout.write(
            self.style.SUCCESS('\n✓ Successfully deleted all existing services and imported new services!')
        )
        if not unsplash_key:
            self.stdout.write(
                self.style.WARNING(
                    '\nNote: Using Picsum Photos for images (no key required). '
                    'Each service gets a unique image based on its keyword. '
                    'For service-specific images, consider using --unsplash-key with an API key from https://unsplash.com/developers'
                )
            )

