"""
ScriptGenius Models - Scripts, hooks, and templates for video creation.
"""

from datetime import datetime

from sqlalchemy import String, Integer, Float, DateTime, Text, JSON, Boolean
from sqlalchemy.orm import Mapped, mapped_column

from backend.database.connection import Base


class Script(Base):
    """Saved video scripts/roteiros."""
    __tablename__ = "scripts"

    id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
    title: Mapped[str] = mapped_column(String(300), default="")
    platform: Mapped[str] = mapped_column(String(20), default="tiktok")
    style: Mapped[str] = mapped_column(String(50), default="tutorial")
    hook: Mapped[str] = mapped_column(Text, default="")
    body: Mapped[str] = mapped_column(Text, default="")
    cta: Mapped[str] = mapped_column(Text, default="")
    full_script: Mapped[str] = mapped_column(Text, default="")
    hashtags: Mapped[dict | None] = mapped_column(JSON, default=list)
    duration_estimate: Mapped[int] = mapped_column(Integer, default=30)
    readability_score: Mapped[float] = mapped_column(Float, default=0.0)
    is_favorite: Mapped[bool] = mapped_column(Boolean, default=False)
    status: Mapped[str] = mapped_column(String(20), default="draft")  # draft, ready, used
    created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
    updated_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)

    def to_dict(self) -> dict:
        return {
            "id": self.id,
            "title": self.title,
            "platform": self.platform,
            "style": self.style,
            "hook": self.hook,
            "body": self.body,
            "cta": self.cta,
            "full_script": self.full_script,
            "hashtags": self.hashtags or [],
            "duration_estimate": self.duration_estimate,
            "readability_score": self.readability_score,
            "is_favorite": self.is_favorite,
            "status": self.status,
            "created_at": self.created_at.isoformat() if self.created_at else None,
            "updated_at": self.updated_at.isoformat() if self.updated_at else None,
        }


class HookEntry(Base):
    """Hook bank - collection of proven opening hooks."""
    __tablename__ = "hook_entries"

    id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
    text: Mapped[str] = mapped_column(Text)
    category: Mapped[str] = mapped_column(String(50), default="geral")  # curiosidade, choque, pergunta, desafio, etc.
    platform: Mapped[str] = mapped_column(String(20), default="all")
    niche: Mapped[str] = mapped_column(String(100), default="geral")
    rating: Mapped[float] = mapped_column(Float, default=0.0)
    times_used: Mapped[int] = mapped_column(Integer, default=0)
    source: Mapped[str] = mapped_column(String(20), default="seed")  # seed, ai, user
    created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)

    def to_dict(self) -> dict:
        return {
            "id": self.id,
            "text": self.text,
            "category": self.category,
            "platform": self.platform,
            "niche": self.niche,
            "rating": self.rating,
            "times_used": self.times_used,
            "source": self.source,
            "created_at": self.created_at.isoformat() if self.created_at else None,
        }


class ScriptTemplate(Base):
    """Script templates for different content types."""
    __tablename__ = "script_templates"

    id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
    name: Mapped[str] = mapped_column(String(200))
    style: Mapped[str] = mapped_column(String(50))
    platform: Mapped[str] = mapped_column(String(20), default="all")
    structure: Mapped[dict | None] = mapped_column(JSON, default=dict)
    example: Mapped[str] = mapped_column(Text, default="")
    description: Mapped[str] = mapped_column(Text, default="")
    created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)

    def to_dict(self) -> dict:
        return {
            "id": self.id,
            "name": self.name,
            "style": self.style,
            "platform": self.platform,
            "structure": self.structure or {},
            "example": self.example,
            "description": self.description,
            "created_at": self.created_at.isoformat() if self.created_at else None,
        }
