from django.db import models
from wagtail.models import Page
from wagtail.fields import RichTextField
from wagtail.admin.panels import FieldPanel, MultiFieldPanel

class HomePage(Page):
    # Hero Section
    greeting = models.CharField(max_length=100, default='Hey, I\'m')
    name = models.CharField(max_length=100, default='Jane Doe')
    tagline = models.TextField(
        default='Welcome to my hub for tech projects, practical tutorials, and personal development.',
        help_text='A brief description of what you do'
    )
    profile_image = models.ForeignKey(
        'wagtailimages.Image',
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name='+'
    )
    primary_button_text = models.CharField(max_length=50, default='View My Work')
    primary_button_url = models.CharField(max_length=100, default='/projects/')
    secondary_button_text = models.CharField(max_length=50, default='Get in Touch')
    secondary_button_url = models.CharField(max_length=100, default='/contact/')
    
    # Featured Projects Section
    show_featured_projects = models.BooleanField(default=True)
    featured_projects_title = models.CharField(max_length=100, default='Featured Projects')
    featured_projects_count = models.PositiveSmallIntegerField(default=3, help_text='Number of projects to display in the featured section')
    
    # Latest Articles Section
    show_latest_articles = models.BooleanField(default=True)
    latest_articles_title = models.CharField(max_length=100, default='Latest Articles')
    
    content_panels = Page.content_panels + [
        MultiFieldPanel([
            FieldPanel('greeting'),
            FieldPanel('name'),
            FieldPanel('tagline'),
            FieldPanel('profile_image'),
            FieldPanel('primary_button_text'),
            FieldPanel('primary_button_url'),
            FieldPanel('secondary_button_text'),
            FieldPanel('secondary_button_url'),
        ], heading='Hero Section'),
        MultiFieldPanel([
            FieldPanel('show_featured_projects'),
            FieldPanel('featured_projects_title'),
            FieldPanel('featured_projects_count'),
        ], heading='Featured Projects Section'),
        MultiFieldPanel([
            FieldPanel('show_latest_articles'),
            FieldPanel('latest_articles_title'),
        ], heading='Latest Articles Section'),
    ]
    
    def get_context(self, request, *args, **kwargs):
        context = super().get_context(request, *args, **kwargs)
        
        # Get featured projects
        if self.show_featured_projects:
            # Import here to avoid circular imports
            from projects.models import ProjectPage
            
            # Get all projects ordered by most recent first
            projects = ProjectPage.objects.live().order_by('-first_published_at')
            
            # Limit to the specified number
            featured_projects = projects[:self.featured_projects_count]
            
            context['featured_projects'] = featured_projects
        
        # Get latest articles
        from blogs.models import BlogPostPage
        context['latest_articles'] = BlogPostPage.objects.live().order_by('-first_published_at')[:3]
        
        return context