"""
Reconciliation exception model for tracking payment/invoice mismatches.
"""
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 ExceptionStatus(str, enum.Enum):
    """Status of a reconciliation exception."""
    open = 'open'
    assigned = 'assigned'
    in_review = 'in_review'
    resolved = 'resolved'
    closed = 'closed'


class ExceptionType(str, enum.Enum):
    """Types of reconciliation exceptions."""
    missing_invoice = 'missing_invoice'
    missing_payment = 'missing_payment'
    amount_mismatch = 'amount_mismatch'
    duplicate_payment = 'duplicate_payment'
    orphaned_payment = 'orphaned_payment'
    date_mismatch = 'date_mismatch'


class ReconciliationException(Base):
    """Exception record for payment/invoice reconciliation mismatches."""
    __tablename__ = 'reconciliation_exceptions'
    
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    
    # Exception details
    exception_type = Column(Enum(ExceptionType), nullable=False, index=True)
    status = Column(Enum(ExceptionStatus), default=ExceptionStatus.open, nullable=False, index=True)
    severity = Column(String(20), default='medium', nullable=False)  # 'low', 'medium', 'high', 'critical'
    
    # 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)
    
    # Exception details
    description = Column(Text, nullable=False)
    expected_amount = Column(Numeric(10, 2), nullable=True)
    actual_amount = Column(Numeric(10, 2), nullable=True)
    difference = Column(Numeric(10, 2), nullable=True)
    
    # Assignment and resolution
    assigned_to = Column(Integer, ForeignKey('users.id'), nullable=True, index=True)
    assigned_at = Column(DateTime, nullable=True)
    resolved_by = Column(Integer, ForeignKey('users.id'), nullable=True)
    resolved_at = Column(DateTime, nullable=True)
    resolution_notes = Column(Text, nullable=True)
    
    # Comments/notes
    comments = Column(JSON, nullable=True)  # Array of comment objects
    
    # Additional metadata
    exception_metadata = Column(JSON, nullable=True)
    
    # Timestamps
    created_at = Column(DateTime, default=datetime.utcnow, nullable=False, index=True)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)
    
    # Relationships
    payment = relationship('Payment', foreign_keys=[payment_id])
    invoice = relationship('Invoice', foreign_keys=[invoice_id])
    booking = relationship('Booking', foreign_keys=[booking_id])
    assignee = relationship('User', foreign_keys=[assigned_to])
    resolver = relationship('User', foreign_keys=[resolved_by])
    
    # Indexes
    __table_args__ = (
        Index('idx_reconciliation_exception_status', 'status', 'created_at'),
        Index('idx_reconciliation_exception_type', 'exception_type', 'status'),
        Index('idx_reconciliation_exception_assigned', 'assigned_to', 'status'),
    )

