"""
Guest complaint management model.
"""
from sqlalchemy import Column, Integer, String, Text, Enum, ForeignKey, DateTime, Boolean, JSON
from sqlalchemy.orm import relationship
from datetime import datetime
import enum
from ...shared.config.database import Base


class ComplaintStatus(str, enum.Enum):
    """Complaint status enumeration."""
    open = 'open'
    in_progress = 'in_progress'
    resolved = 'resolved'
    closed = 'closed'
    escalated = 'escalated'


class ComplaintPriority(str, enum.Enum):
    """Complaint priority enumeration."""
    low = 'low'
    medium = 'medium'
    high = 'high'
    urgent = 'urgent'


class ComplaintCategory(str, enum.Enum):
    """Complaint category enumeration."""
    room_quality = 'room_quality'
    service = 'service'
    cleanliness = 'cleanliness'
    noise = 'noise'
    billing = 'billing'
    staff_behavior = 'staff_behavior'
    amenities = 'amenities'
    other = 'other'


class GuestComplaint(Base):
    """Model for guest complaints and their resolution."""
    __tablename__ = 'guest_complaints'
    
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    
    # Guest information
    guest_id = Column(Integer, ForeignKey('users.id'), nullable=False, index=True)
    booking_id = Column(Integer, ForeignKey('bookings.id'), nullable=True, index=True)
    room_id = Column(Integer, ForeignKey('rooms.id'), nullable=True, index=True)
    
    # Complaint details
    category = Column(Enum(ComplaintCategory), nullable=False, index=True)
    priority = Column(Enum(ComplaintPriority), nullable=False, default=ComplaintPriority.medium, index=True)
    status = Column(Enum(ComplaintStatus), nullable=False, default=ComplaintStatus.open, index=True)
    
    title = Column(String(255), nullable=False)
    description = Column(Text, nullable=False)
    
    # Resolution
    resolution = Column(Text, nullable=True)
    resolved_at = Column(DateTime, nullable=True)
    resolved_by = Column(Integer, ForeignKey('users.id'), nullable=True)
    
    # Assignment
    assigned_to = Column(Integer, ForeignKey('users.id'), nullable=True, index=True)
    escalated_to = Column(Integer, ForeignKey('users.id'), nullable=True)
    
    # Tracking
    created_at = Column(DateTime, default=datetime.utcnow, nullable=False, index=True)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)
    closed_at = Column(DateTime, nullable=True)
    
    # Additional information
    guest_satisfaction_rating = Column(Integer, nullable=True)  # 1-5 rating after resolution
    guest_feedback = Column(Text, nullable=True)
    internal_notes = Column(Text, nullable=True)
    attachments = Column(JSON, nullable=True)  # Array of file paths/URLs
    
    # Follow-up
    requires_follow_up = Column(Boolean, nullable=False, default=False)
    follow_up_date = Column(DateTime, nullable=True)
    follow_up_completed = Column(Boolean, nullable=False, default=False)
    
    # Relationships
    guest = relationship('User', foreign_keys=[guest_id])
    booking = relationship('Booking', foreign_keys=[booking_id])
    room = relationship('Room', foreign_keys=[room_id])
    assignee = relationship('User', foreign_keys=[assigned_to])
    resolver = relationship('User', foreign_keys=[resolved_by])
    escalator = relationship('User', foreign_keys=[escalated_to])


class ComplaintUpdate(Base):
    """Model for tracking complaint updates and communication."""
    __tablename__ = 'complaint_updates'
    
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    complaint_id = Column(Integer, ForeignKey('guest_complaints.id'), nullable=False, index=True)
    
    # Update details
    update_type = Column(String(50), nullable=False)  # 'status_change', 'assignment', 'note', 'resolution'
    description = Column(Text, nullable=False)
    
    # Who made the update
    updated_by = Column(Integer, ForeignKey('users.id'), nullable=False)
    
    # Timestamp
    created_at = Column(DateTime, default=datetime.utcnow, nullable=False, index=True)
    
    # Additional data
    update_metadata = Column(JSON, nullable=True)  # Store additional context
    
    # Relationships
    complaint = relationship('GuestComplaint', backref='updates')
    updater = relationship('User', foreign_keys=[updated_by])

