"""Audit log for compliance and tracking."""
from datetime import datetime
from sqlalchemy import Column, Integer, String, DateTime, ForeignKey, JSON, Text
from sqlalchemy.orm import relationship
from app.core.database import Base

class AuditLog(Base):
    __tablename__ = "audit_logs"
    
    id = Column(Integer, primary_key=True, index=True)
    organization_id = Column(Integer, ForeignKey("organizations.id"))
    user_id = Column(Integer, ForeignKey("users.id"))
    
    # Action details
    action = Column(String(100), nullable=False, index=True)  # user.login, issue.created, integration.updated
    resource_type = Column(String(50))  # user, issue, integration, etc
    resource_id = Column(Integer)
    
    # Context
    ip_address = Column(String(45))
    user_agent = Column(String(500))
    
    # Changes
    old_values = Column(JSON)
    new_values = Column(JSON)
    description = Column(Text)
    
    # Status
    success = Column(String(10), default="success")  # success, failure
    error_message = Column(String(500))
    
    created_at = Column(DateTime, default=datetime.utcnow, index=True)
    
    # Relationships
    organization = relationship("Organization", back_populates="audit_logs")
    user = relationship("User", back_populates="audit_logs")
