"""Models for download task persistence with SQLModel support"""
import uuid
from typing import Optional
from datetime import datetime
from sqlmodel import SQLModel, Field, Column, String
from enum import Enum


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


class DownloadTaskTable(SQLModel, table=True):
    """Database table for persisting download tasks across server restarts."""
    __tablename__ = "download_tasks"

    id: str = Field(
        default_factory=lambda: str(uuid.uuid4()),
        primary_key=True,
        index=True,
        nullable=False,
    )
    url: str = Field(default="", sa_column=Column(String))
    filename: str = Field(sa_column=Column(String))
    host: str = Field(default="other", sa_column=Column(String))
    status: str = Field(default="pending", sa_column=Column(String))
    progress: float = Field(default=0.0)
    downloaded_bytes: int = Field(default=0)
    total_bytes: Optional[int] = Field(default=None)
    speed: float = Field(default=0.0)
    error: Optional[str] = Field(default=None, sa_column=Column(String))
    created_at: datetime = Field(default_factory=datetime.now)
    started_at: Optional[datetime] = Field(default=None)
    completed_at: Optional[datetime] = Field(default=None)
    file_path: Optional[str] = Field(default=None, sa_column=Column(String))
