from pydantic import BaseModel
from enum import Enum
from typing import Optional
from datetime import datetime


class DownloadStatus(str, Enum):
    PENDING = "pending"
    DOWNLOADING = "downloading"
    PAUSED = "paused"
    COMPLETED = "completed"
    FAILED = "failed"
    CANCELLED = "cancelled"


class HostType(str, Enum):
    RAPIDFILE = "rapidfile"
    UNFICHIER = "1fichier"
    DOODSTREAM = "doodstream"
    OTHER = "other"


class DownloadTask(BaseModel):
    id: str
    url: str
    filename: str
    host: HostType
    status: DownloadStatus
    progress: float = 0.0
    downloaded_bytes: int = 0
    total_bytes: Optional[int] = None
    speed: float = 0.0
    error: Optional[str] = None
    created_at: datetime
    started_at: Optional[datetime] = None
    completed_at: Optional[datetime] = None
    file_path: Optional[str] = None


class DownloadRequest(BaseModel):
    url: str
    filename: Optional[str] = None


class AnimeMetadata(BaseModel):
    """Metadata for anime series"""
    synopsis: Optional[str] = None
    genres: list[str] = []
    rating: Optional[str] = None  # Could be "PG-13", "R", etc., or numeric like "8.5/10"
    release_year: Optional[int] = None
    studio: Optional[str] = None
    poster_image: Optional[str] = None
    banner_image: Optional[str] = None
    total_episodes: Optional[int] = None
    status: Optional[str] = None  # "Ongoing", "Completed", etc.
    alternative_titles: list[str] = []


class AnimeSearchResult(BaseModel):
    """Enhanced search result with metadata"""
    title: str
    url: str
    cover_image: Optional[str] = None
    type: str  # "search_result" or "direct"
    metadata: Optional[AnimeMetadata] = None

# Import all SQLModel tables here to ensure they are registered together
from .auth import UserTable
from .watchlist import WatchlistItemTable, WatchlistSettingsTable
from .favorites import FavoriteTable
from .sonarr import SonarrMappingTable, SonarrConfigTable
from .settings import AppSettingsTable
