from sqlalchemy import Column, BigInteger, String, Text, DateTime, Index, func
from datetime import datetime
from enum import Enum as PyEnum
from app.db.session import Base


class EntityType(str, PyEnum):
    """Entity types that can have journal entries"""
    PPR = "PPR"
    LOCAL_FLIGHT = "LOCAL_FLIGHT"
    ARRIVAL = "ARRIVAL"
    DEPARTURE = "DEPARTURE"
    OVERFLIGHT = "OVERFLIGHT"


class JournalEntry(Base):
    """
    Generic journal table for tracking changes across all entity types.
    Replaces the PPR-specific journal table.
    """
    __tablename__ = "journal"
    
    id = Column(BigInteger, primary_key=True, autoincrement=True)
    entity_type = Column(String(50), nullable=False, index=True)  # PPR, LOCAL_FLIGHT, ARRIVAL, DEPARTURE
    entity_id = Column(BigInteger, nullable=False, index=True)  # ID of the entity
    entry = Column(Text, nullable=False)
    user = Column(String(50), nullable=False, index=True)
    ip = Column(String(45), nullable=True)  # Made optional for new entries
    entry_dt = Column(DateTime, nullable=False, server_default=func.current_timestamp(), index=True)
    
    # Composite index for efficient queries
    __table_args__ = (
        Index('idx_entity_lookup', 'entity_type', 'entity_id'),
    )
