from django.db import models


class AboutPage(models.Model):
    """Singleton model for About Us page content"""
    
    # Page header
    hero_title = models.CharField(max_length=300, default="About NewsHub")
    hero_description = models.TextField(default="Your trusted source for breaking news and stories that matter.")
    
    # Our Story section
    our_story_title = models.CharField(max_length=300, default="Our Story")
    our_story_paragraph1 = models.TextField(
        default="Founded in 2024, NewsHub was born from a simple idea: everyone deserves access to accurate, unbiased, and timely news. We started as a small team of passionate journalists and technologists who believed that quality journalism could thrive in the digital age.",
        help_text="First paragraph of our story"
    )
    our_story_paragraph2 = models.TextField(
        default="Today, we serve millions of readers worldwide, delivering news across entertainment, society, politics, world events, and sports. Our commitment to truth and integrity remains unwavering.",
        help_text="Second paragraph of our story"
    )
    our_story_paragraph3 = models.TextField(
        blank=True,
        help_text="Optional third paragraph of our story"
    )
    
    # Our Coverage section
    coverage_title = models.CharField(max_length=300, default="Our Coverage")
    coverage_description = models.TextField(default="We cover the stories that matter across multiple categories:")
    coverage_json = models.TextField(
        blank=True,
        help_text='Coverage categories as JSON array: [{"icon": "📺", "title": "Entertainment", "description": "..."}]'
    )
    
    # What We Do section
    what_we_do_title = models.CharField(max_length=300, default="What We Do")
    what_we_do_json = models.TextField(
        blank=True,
        help_text='What we do items as JSON array: ["Breaking news alerts and live updates", ...]'
    )
    
    # Our Values section
    values_title = models.CharField(max_length=300, default="Our Values")
    values_json = models.TextField(
        blank=True,
        help_text='Core values as JSON array: [{"icon": "✓", "title": "Accuracy", "description": "..."}]'
    )
    
    # CTA section
    cta_title = models.CharField(max_length=300, default="Get Involved")
    cta_description = models.TextField(default="Stay connected with NewsHub for the latest news and updates.")
    cta_button_text = models.CharField(max_length=100, default="Read Latest News")
    cta_button_url = models.CharField(max_length=200, default="/")
    
    updated_at = models.DateTimeField(auto_now=True)
    
    class Meta:
        verbose_name = 'About Page Settings'
        verbose_name_plural = 'About Page Settings'
    
    def __str__(self):
        return "About Page Settings"
    
    def save(self, *args, **kwargs):
        self.pk = 1
        super().save(*args, **kwargs)
    
    @classmethod
    def load(cls):
        obj, created = cls.objects.get_or_create(pk=1)
        return obj
    
    def get_coverage(self):
        """Parse coverage JSON and return as list"""
        import json
        try:
            return json.loads(self.coverage_json)
        except (json.JSONDecodeError, TypeError):
            return [
                {"icon": "📺", "title": "Entertainment", "description": "Latest updates from movies, music, celebrities, and pop culture worldwide."},
                {"icon": "👥", "title": "Society", "description": "Stories about communities, social issues, and human interest that impact lives."},
                {"icon": "🏛️", "title": "Politics", "description": "Political news, government policies, and analysis of current affairs."},
                {"icon": "🌍", "title": "World", "description": "International news, global events, and stories from across the planet."},
                {"icon": "⚽", "title": "Sport", "description": "Sports news, match updates, player profiles, and athletic achievements."},
            ]
    
    def get_values(self):
        """Parse values JSON and return as list"""
        import json
        try:
            return json.loads(self.values_json)
        except (json.JSONDecodeError, TypeError):
            return [
                {"icon": "✓", "title": "Accuracy", "description": "We fact-check every story to ensure the information we provide is correct and reliable."},
                {"icon": "🛡️", "title": "Integrity", "description": "We maintain the highest ethical standards in our reporting and operations."},
                {"icon": "🔓", "title": "Independence", "description": "Our editorial decisions are made free from external influence or bias."},
                {"icon": "👁️", "title": "Transparency", "description": "We are open about our sources and methods of reporting."},
            ]
    
    def get_what_we_do(self):
        """Parse what_we_do JSON and return as list"""
        import json
        try:
            return json.loads(self.what_we_do_json)
        except (json.JSONDecodeError, TypeError):
            return [
                "Breaking news alerts and live updates",
                "In-depth investigative reporting",
                "Expert analysis and commentary",
                "Multimedia content including photos and videos",
                "Community-focused local stories",
                "International news perspectives",
            ]


class TeamMember(models.Model):
    """Team members for About Us page"""
    name = models.CharField(max_length=200)
    role = models.CharField(max_length=200)
    bio = models.TextField(blank=True)
    photo = models.ImageField(upload_to='site/team/', blank=True, null=True)
    order = models.IntegerField(default=0, help_text="Display order on About page")
    is_active = models.BooleanField(default=True)
    
    class Meta:
        verbose_name = 'Team Member'
        verbose_name_plural = 'Team Members'
        ordering = ['order', 'name']
    
    def __str__(self):
        return f"{self.name} - {self.role}"