"""
Financial audit trail model for tracking all financial transactions.
This ensures compliance and provides a complete audit log for financial operations.
"""
from sqlalchemy import Column, Integer, String, DateTime, Numeric, Text, Enum, ForeignKey, JSON, Index
from sqlalchemy.orm import relationship
from datetime import datetime
import enum
from ...shared.config.database import Base


class FinancialActionType(str, enum.Enum):
    """Types of financial actions that can be audited."""
    payment_created = 'payment_created'
    payment_completed = 'payment_completed'
    payment_refunded = 'payment_refunded'
    payment_failed = 'payment_failed'
    invoice_created = 'invoice_created'
    invoice_updated = 'invoice_updated'
    invoice_paid = 'invoice_paid'
    refund_processed = 'refund_processed'
    price_modified = 'price_modified'
    discount_applied = 'discount_applied'
    promotion_applied = 'promotion_applied'
    settings_changed = 'settings_changed'  # Financial settings (tax_rate, currency, etc.)
    data_exported = 'data_exported'  # Financial data exports (CSV, reports)


class FinancialAuditTrail(Base):
    """Audit trail for all financial transactions and modifications."""
    __tablename__ = 'financial_audit_trail'
    
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    
    # Action details
    action_type = Column(Enum(FinancialActionType), nullable=False, index=True)
    action_description = Column(Text, nullable=False)
    
    # Related entities
    payment_id = Column(Integer, ForeignKey('payments.id'), nullable=True, index=True)
    invoice_id = Column(Integer, ForeignKey('invoices.id'), nullable=True, index=True)
    booking_id = Column(Integer, ForeignKey('bookings.id'), nullable=True, index=True)
    
    # Financial details
    amount = Column(Numeric(10, 2), nullable=True)
    previous_amount = Column(Numeric(10, 2), nullable=True)
    currency = Column(String(3), nullable=True, default='USD')
    
    # User information
    performed_by = Column(Integer, ForeignKey('users.id'), nullable=False, index=True)
    performed_by_email = Column(String(255), nullable=True)  # Store email for audit even if user deleted
    
    # Additional context
    audit_metadata = Column(JSON, nullable=True)  # Store additional context (IP, user agent, etc.)
    notes = Column(Text, nullable=True)
    
    # Timestamp
    created_at = Column(DateTime, default=datetime.utcnow, nullable=False, index=True)
    
    # Relationships
    payment = relationship('Payment', foreign_keys=[payment_id])
    invoice = relationship('Invoice', foreign_keys=[invoice_id])
    booking = relationship('Booking', foreign_keys=[booking_id])
    user = relationship('User', foreign_keys=[performed_by])
    
    # Indexes for common queries
    __table_args__ = (
        Index('idx_financial_audit_created', 'created_at'),
        Index('idx_financial_audit_action', 'action_type', 'created_at'),
        Index('idx_financial_audit_user', 'performed_by', 'created_at'),
        Index('idx_financial_audit_booking', 'booking_id', 'created_at'),
    )

