# app/models/memory/consolidation.py
"""
Memory Consolidation Log database model.

Tracks memory consolidation jobs that transfer knowledge
between memory tiers.
"""

from sqlalchemy import Column, DateTime, Enum, Index, Integer, Text

from app.models.base import Base, TimestampMixin, UUIDMixin

from .enums import ConsolidationStatus, ConsolidationType


class MemoryConsolidationLog(Base, UUIDMixin, TimestampMixin):
    """
    Memory consolidation job log.

    Tracks consolidation operations:
    - Working -> Episodic (session end)
    - Episodic -> Semantic (fact extraction)
    - Episodic -> Procedural (procedure learning)
    - Pruning (removing low-value memories)
    """

    __tablename__ = "memory_consolidation_log"

    # Consolidation type
    consolidation_type: Column[ConsolidationType] = Column(
        Enum(ConsolidationType),
        nullable=False,
        index=True,
    )

    # Counts
    source_count = Column(Integer, nullable=False, default=0)
    result_count = Column(Integer, nullable=False, default=0)

    # Timing
    started_at = Column(DateTime(timezone=True), nullable=False)
    completed_at = Column(DateTime(timezone=True), nullable=True)

    # Status
    status: Column[ConsolidationStatus] = Column(
        Enum(ConsolidationStatus),
        nullable=False,
        default=ConsolidationStatus.PENDING,
        index=True,
    )

    # Error details if failed
    error = Column(Text, nullable=True)

    __table_args__ = (
        # Query patterns
        Index("ix_consolidation_type_status", "consolidation_type", "status"),
        Index("ix_consolidation_started", "started_at"),
    )

    @property
    def duration_seconds(self) -> float | None:
        """Calculate duration of the consolidation job."""
        if self.completed_at is None or self.started_at is None:
            return None
        return (self.completed_at - self.started_at).total_seconds()

    def __repr__(self) -> str:
        return (
            f"<MemoryConsolidationLog {self.id} "
            f"type={self.consolidation_type.value} status={self.status.value}>"
        )
