import os
from django.core.management.base import BaseCommand
from base.models import Page, PageSectionType, PageSection, PageSectionItem

class Command(BaseCommand):
    help = 'Seed database with sample education platform data'

    def handle(self, *args, **options):
        self.stdout.write('Seeding education platform data...')
        
        # Create section types
        section_types = self.create_section_types()
        
        # Create pages
        pages = self.create_pages()
        
        # Create page sections
        self.create_page_sections(pages, section_types)
        
        self.stdout.write(
            self.style.SUCCESS('Successfully seeded education platform data!')
        )

    def create_section_types(self):
        """Create all section types"""
        section_types_data = [
            {"name": "Hero"},
            {"name": "Features"},
            {"name": "Course Categories"},
            {"name": "Popular Courses"},
            {"name": "Instructors"},
            {"name": "Testimonials"},
            {"name": "Statistics"},
            {"name": "FAQ"},
            {"name": "Call to Action"},
            {"name": "Learning Path"},
            {"name": "Pricing"},
            {"name": "Blog Highlights"},
        ]
        
        section_types = {}
        for data in section_types_data:
            section_type, created = PageSectionType.objects.get_or_create(
                name=data["name"]
            )
            section_types[data["name"]] = section_type
            if created:
                self.stdout.write(f'Created section type: {data["name"]}')
        
        return section_types

    def create_pages(self):
        """Create main pages"""
        pages_data = [
            {
                "name": "Home",
                "slug": "home",
                "title": "LearnHub - Online Learning Platform",
                "description": "<p>Your gateway to knowledge and skills development</p>",
                "meta_description": "LearnHub offers the best online courses from top instructors. Start your learning journey today.",
                "is_active": True,
                "order": 0
            },
            {
                "name": "Courses",
                "slug": "courses",
                "title": "All Courses - LearnHub",
                "description": "<p>Browse our comprehensive catalog of courses</p>",
                "meta_description": "Explore hundreds of courses in programming, design, business, and more.",
                "is_active": True,
                "order": 1
            },
            {
                "name": "About Us",
                "slug": "about",
                "title": "About LearnHub",
                "description": "<p>Learn about our mission and vision for education</p>",
                "meta_description": "Discover how LearnHub is transforming online education with quality courses.",
                "is_active": True,
                "order": 2
            },
            {
                "name": "Contact",
                "slug": "contact",
                "title": "Contact Us - LearnHub",
                "description": "<p>Get in touch with our team</p>",
                "meta_description": "Contact LearnHub for support, partnerships, or any questions about our platform.",
                "is_active": True,
                "order": 3
            },
            {
                "name": "Pricing",
                "slug": "pricing",
                "title": "Pricing Plans - LearnHub",
                "description": "<p>Choose the right plan for your learning needs</p>",
                "meta_description": "Affordable pricing plans for individuals, students, and organizations.",
                "is_active": True,
                "order": 4
            }
        ]
        
        pages = {}
        for data in pages_data:
            page, created = Page.objects.get_or_create(
                slug=data["slug"],
                defaults={
                    "name": data["name"],
                    "title": data["title"],
                    "description": data["description"],
                    "meta_description": data["meta_description"],
                    "is_active": data["is_active"],
                    "order": data["order"]
                }
            )
            pages[data["slug"]] = page
            if created:
                self.stdout.write(f'Created page: {data["name"]}')
        
        return pages

    def create_page_sections(self, pages, section_types):
        """Create page sections with items"""
        
        # Home Page Sections
        home_sections = [
            {
                "name": "main_hero",
                "section_type": section_types["Hero"],
                "title": "Learn Without Limits",
                "subtitle": "Start, switch, or advance your career with thousands of courses",
                "description": "<p>Join over 10 million learners worldwide and gain new skills with our expert-led courses</p>",
                "content_alignment": "center",
                "background_color": "#1a365d",
                "text_color": "#ffffff",
                "order": 0,
                "key_points": ["5000+ Courses", "Expert Instructors", "Lifetime Access", "Certificate Included"],
                "metadata": {"show_search": True, "button_variant": "primary"}
            },
            {
                "name": "features_section",
                "section_type": section_types["Features"],
                "title": "Why Choose LearnHub?",
                "subtitle": "We provide the best learning experience",
                "content_alignment": "center",
                "order": 1,
                "key_points": []
            },
            {
                "name": "popular_courses",
                "section_type": section_types["Popular Courses"],
                "title": "Most Popular Courses",
                "subtitle": "Trending courses loved by our students",
                "content_alignment": "left",
                "order": 2,
                "key_points": []
            },
            {
                "name": "statistics_section",
                "section_type": section_types["Statistics"],
                "title": "Our Impact in Numbers",
                "content_alignment": "center",
                "background_color": "#f7fafc",
                "order": 3,
                "key_points": [],
                "metadata": {"layout": "4-column"}
            }
        ]
        
        # Create home page sections
        for section_data in home_sections:
            section, created = PageSection.objects.get_or_create(
                page=pages["home"],
                name=section_data["name"],
                defaults=section_data
            )
            if created:
                self.stdout.write(f'Created section: {section_data["name"]}')
                
                # Add items for specific sections
                if section_data["name"] == "features_section":
                    self.create_feature_items(section)
                elif section_data["name"] == "popular_courses":
                    self.create_course_items(section)
                elif section_data["name"] == "statistics_section":
                    self.create_statistic_items(section)
        
        # Courses Page Sections
        courses_sections = [
            {
                "name": "courses_hero",
                "section_type": section_types["Hero"],
                "title": "Explore Our Courses",
                "subtitle": "Find the perfect course to advance your career",
                "content_alignment": "left",
                "order": 0,
                "key_points": ["All Skill Levels", "Self-Paced Learning", "Hands-on Projects"]
            },
            {
                "name": "course_categories",
                "section_type": section_types["Course Categories"],
                "title": "Browse by Category",
                "content_alignment": "left",
                "order": 1,
                "key_points": []
            }
        ]
        
        for section_data in courses_sections:
            section, created = PageSection.objects.get_or_create(
                page=pages["courses"],
                name=section_data["name"],
                defaults=section_data
            )
            if created:
                self.stdout.write(f'Created section: {section_data["name"]}')
                if section_data["name"] == "course_categories":
                    self.create_category_items(section)

    def create_feature_items(self, section):
        """Create feature items for features section"""
        features = [
            {
                "title": "Expert Instructors",
                "subtitle": "Learn from industry professionals",
                "description": "<p>Our instructors are experienced professionals who bring real-world knowledge to the classroom.</p>",
                "icon": "👨‍🏫",
                "order": 0,
                "key_points": ["Industry Experts", "Practical Knowledge", "Mentorship"]
            },
            {
                "title": "Flexible Learning",
                "subtitle": "Learn at your own pace",
                "description": "<p>Access courses anytime, anywhere with our mobile-friendly platform.</p>",
                "icon": "⏰",
                "order": 1,
                "key_points": ["Self-Paced", "Mobile Access", "Lifetime Access"]
            },
            {
                "title": "Hands-on Projects",
                "subtitle": "Build real portfolio projects",
                "description": "<p>Apply your knowledge with practical projects and build your portfolio.</p>",
                "icon": "💻",
                "order": 2,
                "key_points": ["Real Projects", "Portfolio Ready", "Code Reviews"]
            },
            {
                "title": "Career Support",
                "subtitle": "Get help landing your dream job",
                "description": "<p>We provide career services including resume reviews and interview preparation.</p>",
                "icon": "🎯",
                "order": 3,
                "key_points": ["Resume Help", "Interview Prep", "Job Portal"]
            }
        ]
        
        for feature in features:
            PageSectionItem.objects.create(
                section=section,
                **feature
            )
        self.stdout.write(f'Created {len(features)} feature items')

    def create_course_items(self, section):
        """Create popular course items"""
        courses = [
            {
                "title": "Python Programming Masterclass",
                "subtitle": "From beginner to advanced",
                "description": "<p>Learn Python programming with hands-on projects and build real applications.</p>",
                "icon": "🐍",
                "order": 0,
                "button_text": "View Course",
                "button_url": "/courses/python-masterclass",
                "key_points": ["48 hours", "Beginner Friendly", "Certificate"]
            },
            {
                "title": "Web Development Bootcamp",
                "subtitle": "Full-stack development",
                "description": "<p>Become a full-stack developer with HTML, CSS, JavaScript, and React.</p>",
                "icon": "🌐",
                "order": 1,
                "button_text": "View Course",
                "button_url": "/courses/web-development",
                "key_points": ["60 hours", "Projects", "Career Guidance"]
            },
            {
                "title": "Data Science Fundamentals",
                "subtitle": "Python, Pandas, and Machine Learning",
                "description": "<p>Master data analysis and visualization with Python and popular libraries.</p>",
                "icon": "📊",
                "order": 2,
                "button_text": "View Course",
                "button_url": "/courses/data-science",
                "key_points": ["52 hours", "Real Datasets", "ML Projects"]
            }
        ]
        
        for course in courses:
            PageSectionItem.objects.create(
                section=section,
                **course
            )
        self.stdout.write(f'Created {len(courses)} course items')

    def create_statistic_items(self, section):
        """Create statistic items"""
        stats = [
            {
                "title": "10,000+",
                "subtitle": "Active Students",
                "description": "<p>Join our growing community of learners</p>",
                "order": 0
            },
            {
                "title": "500+",
                "subtitle": "Expert Instructors",
                "description": "<p>Learn from industry professionals</p>",
                "order": 1
            },
            {
                "title": "5,000+",
                "subtitle": "Courses Available",
                "description": "<p>Wide range of topics and skills</p>",
                "order": 2
            },
            {
                "title": "98%",
                "subtitle": "Satisfaction Rate",
                "description": "<p>Students recommend our courses</p>",
                "order": 3
            }
        ]
        
        for stat in stats:
            PageSectionItem.objects.create(
                section=section,
                **stat
            )
        self.stdout.write(f'Created {len(stats)} statistic items')

    def create_category_items(self, section):
        """Create course category items"""
        categories = [
            {
                "title": "Programming",
                "subtitle": "1500+ courses",
                "description": "<p>Learn programming languages and software development</p>",
                "icon": "💻",
                "order": 0,
                "button_text": "Explore",
                "button_url": "/courses/programming"
            },
            {
                "title": "Data Science",
                "subtitle": "800+ courses",
                "description": "<p>Master data analysis, visualization, and machine learning</p>",
                "icon": "📊",
                "order": 1,
                "button_text": "Explore",
                "button_url": "/courses/data-science"
            },
            {
                "title": "Business",
                "subtitle": "1200+ courses",
                "description": "<p>Develop business skills and management expertise</p>",
                "icon": "💼",
                "order": 2,
                "button_text": "Explore",
                "button_url": "/courses/business"
            }
        ]
        
        for category in categories:
            PageSectionItem.objects.create(
                section=section,
                **category
            )
        self.stdout.write(f'Created {len(categories)} category items')