"""
Logging model for storing application logs.

The ``Journal`` model used to live here too but was moved to
``journal.py`` for discoverability — it's unrelated to the
``ResearchLog`` table this file owns. Compat imports of
``from ...database.models.logs import Journal`` still work via the
re-export below.
"""

from sqlalchemy import (
    Column,
    ForeignKey,
    Integer,
    Sequence,
    String,
    Text,
)
from sqlalchemy_utc import UtcDateTime, utcnow

from .base import Base
from .journal import Journal  # noqa: F401 — compat re-export


class ResearchLog(Base):
    """
    Logging table for all research operations.

    All logging from research operations, including debug messages,
    errors, and milestones are stored here.
    """

    __tablename__ = "app_logs"

    id = Column(Integer, Sequence("reseach_log_id_seq"), primary_key=True)

    timestamp = Column(UtcDateTime, server_default=utcnow(), nullable=False)
    message = Column(Text, nullable=False)
    # Module that the log message came from.
    module = Column(Text, nullable=False)
    # Function that the log message came from.
    function = Column(Text, nullable=False)
    # Line number that the log message came from.
    line_no = Column(Integer, nullable=False)
    # Log level.
    level = Column(String(32), nullable=False)
    research_id = Column(
        String(36),  # UUID as string
        ForeignKey("research_history.id", ondelete="CASCADE"),
        nullable=True,
        index=True,
    )

    def __repr__(self):
        return f"<ResearchLog({self.level}: '{self.message[:50]}...')>"
