"""
Activity Post Model for NGO Foundation.
Fulfills the Activity Post Management Module requirement from the specification.
Provides dynamic, Facebook-style activity posts showcasing photos, field operations,
dates, and relief activities to establish public trust and complete transparency.
All code contains proper English comments for clarity and maintenance.
"""

from datetime import date, datetime, timezone
import json
from app.extensions import db


class ActivityPost(db.Model):
    """
    Represents an official activity update, field operation, or relief drive
    published to the live dynamic activity feed on the NGO portal.
    """
    __tablename__ = 'activity_posts'

    id = db.Column(db.Integer, primary_key=True)
    
    # Core Post Metadata
    title = db.Column(db.String(200), nullable=False, index=True)
    caption = db.Column(db.Text, nullable=False)
    activity_date = db.Column(db.Date, nullable=False, default=date.today, index=True)
    location = db.Column(db.String(150), nullable=True)  # e.g., "Trilokpuri, East Delhi"
    
    # Category / Pillar classification
    category = db.Column(db.String(80), nullable=False, default='Relief Drive', index=True)
    # Options: Relief Drive, Health Camp, Education Support, Food Distribution, Community Event, Disaster Response, Celebration
    
    # Media: Featured photo path or URL, plus optional additional photos as JSON
    primary_image_url = db.Column(db.String(350), nullable=False)
    additional_images_json = db.Column(db.Text, nullable=True)  # JSON-encoded array of extra image URLs
    
    # Engagement and Priority
    likes_count = db.Column(db.Integer, nullable=False, default=0)
    shares_count = db.Column(db.Integer, nullable=False, default=0)
    is_featured = db.Column(db.Boolean, nullable=False, default=False)  # Pin to top of feed
    status = db.Column(db.String(30), nullable=False, default='published')  # published, draft, archived
    
    author_name = db.Column(db.String(120), nullable=False, default='NGO Foundation Team')
    
    # Timestamps for audit tracking
    created_at = db.Column(db.DateTime(timezone=True), nullable=False, default=lambda: datetime.now(timezone.utc))
    updated_at = db.Column(db.DateTime(timezone=True), nullable=True, onupdate=lambda: datetime.now(timezone.utc))

    @property
    def additional_images(self) -> list:
        """Returns extra image URLs parsed from stored JSON."""
        if not self.additional_images_json:
            return []
        try:
            return json.loads(self.additional_images_json)
        except Exception:
            return []

    def __repr__(self):
        return f"<ActivityPost #{self.id} - {self.title}>"
