# /opt/docker/dev/service_finder/backend/app/models/vehicle/history.py
import uuid
import enum
from datetime import datetime, date
from typing import Optional, Any
from sqlalchemy import String, DateTime, ForeignKey, JSON, Date, Text, Integer
from sqlalchemy.dialects.postgresql import ENUM as PG_ENUM, UUID as PG_UUID
from sqlalchemy.orm import Mapped, mapped_column, relationship
from sqlalchemy.sql import func

# MB 2.0: Központi aszinkron adatbázis motorból húzzuk be a Base-t
from app.database import Base

class LogSeverity(str, enum.Enum):
    info = "info"
    warning = "warning"
    critical = "critical"
    emergency = "emergency"


class AuditLog(Base):
    """ Rendszerszintű műveletnapló. """
    __tablename__ = "audit_logs"
    __table_args__ = {"schema": "audit"}
    
    id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True)
    
    # MB 2.0 JAVÍTÁS: A felhasználó az identity sémában lakik!
    user_id: Mapped[Optional[int]] = mapped_column(Integer, ForeignKey("identity.users.id"))
    
    severity: Mapped[LogSeverity] = mapped_column(
        PG_ENUM(LogSeverity, name="log_severity", schema="audit"),
        default=LogSeverity.info
    )
    
    action: Mapped[str] = mapped_column(String(100), index=True) 
    target_type: Mapped[Optional[str]] = mapped_column(String(50), index=True) 
    target_id: Mapped[Optional[str]] = mapped_column(String(50), index=True)
    
    old_data: Mapped[Optional[Any]] = mapped_column(JSON)
    new_data: Mapped[Optional[Any]] = mapped_column(JSON)
    
    ip_address: Mapped[Optional[str]] = mapped_column(String(45), index=True)
    user_agent: Mapped[Optional[Text]] = mapped_column(Text)
    timestamp: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now(), index=True)
    
    user: Mapped[Optional["User"]] = relationship("User") 