"""Models for article and video processing.

This module contains the data models used for handling articles and videos
in the content processing pipeline.
"""

from dataclasses import asdict, dataclass, field
from datetime import datetime
from typing import Optional, List, Any, Dict, ClassVar, Type, TypeVar

T = TypeVar('T', bound='BaseModel')

@dataclass
class BaseModel:
    """Base model class with common functionality for all models."""

    def to_dict(self) -> Dict[str, Any]:
        """Convert the instance to a dictionary.
        
        Returns:
            Dict[str, Any]: Dictionary representation of the instance
        """
        return asdict(self)
    
    @classmethod
    def from_dict(cls: Type[T], data: Dict[str, Any]) -> T:
        """Create an instance from a dictionary.
        
        Args:
            data: Dictionary containing model data
            
        Returns:
            An instance of the model
        """
        return cls(**data)


@dataclass
class Article(BaseModel):
    """Represents an article with its metadata and processing status.
    
    Attributes:
        unique_id: Unique identifier for the article
        url: Original URL of the article
        title: Title of the article
        keywords: Keywords associated with the article
        description: Brief description of the article
        text_content: Full text content of the article
        src: Source of the article
        published_date: Date when the article was published
        content_url: URL to the content
        ai_category: Primary category assigned by AI
        ai_summary: Summary generated by AI
        ai_category2: Secondary category assigned by AI
        img_url: URL to the article's image
        video_made: Whether a video has been made for this article
        video_url: URL to the video if available
        video_by: Creator of the video
        hl: Headline (shortened version)
        fb_posted: Whether the article was posted to Facebook
        related_articles: List of related articles
        message: Additional message/notes about the article
        has_img: Whether the article has an image
        has_video: Whether the article has a video
        created_at: Timestamp when the article was created
    """
    
    unique_id: str
    url: str
    title: str
    keywords: str
    description: str
    text_content: str
    src: str
    published_date: datetime
    content_url: str
    ai_category: Optional[str] = None
    ai_summary: Optional[str] = None
    ai_category2: Optional[str] = None
    img_url: Optional[str] = None
    video_made: bool = False
    video_url: Optional[str] = None
    video_by: Optional[str] = None
    hl: Optional[str] = field(default=None)
    fb_posted: Optional[bool] = None
    related_articles: Optional[List[Any]] = None
    message: Optional[str] = None
    has_img: Optional[bool] = None
    has_video: Optional[bool] = None
    created_at: Optional[datetime] = None
    
    def __post_init__(self):
        """Perform post-initialization processing."""
        # If img_url is set but has_img is not, set has_img based on img_url
        if self.has_img is None:
            self.has_img = bool(self.img_url)
            
        # If video_url is set but has_video is not, set has_video based on video_url
        if self.has_video is None:
            self.has_video = bool(self.video_url)


@dataclass
class Video(BaseModel):
    """Represents a video with its processing metadata.
    
    Attributes:
        unique_id: Unique identifier for the video
        target_dir: Directory where the video is/will be stored
        article: Associated article ID if this video is based on an article
        channel_name: Name of the channel where the video will be published
        channel_id: ID of the channel where the video will be published
        is_vertical: Whether the video is in vertical format
        ads: Information about ads for this video
    """

    unique_id: str
    target_dir: Optional[str] = None
    article: Optional[str] = None
    channel_name: Optional[str] = None
    channel_id: Optional[str] = None
    is_vertical: bool = False
    ads: Optional[str] = None


@dataclass
class FacebookPost(BaseModel):
    """Represents a Facebook post for content distribution.
    
    Attributes:
        unique_id: Unique identifier for the post
        profile: Profile/page where the post will be published
        title: Title of the post
        summary: Summary text for the post
        link: External link to be included in the post
        is_master: Whether this is a master/original post
        fb_url: URL to the Facebook post after publishing
        bg_idx: Background image index if applicable
    """
    unique_id: str
    profile: str
    title: Optional[str] = None
    summary: Optional[str] = None
    link: Optional[str] = None
    is_master: bool = False
    fb_url: Optional[str] = None
    bg_idx: Optional[int] = None