"""
Webhook models for external integrations.
"""
from sqlalchemy import Column, Integer, String, Text, DateTime, ForeignKey, Boolean, Enum, JSON
from sqlalchemy.orm import relationship
from datetime import datetime
import enum
from ...shared.config.database import Base

class WebhookEventType(str, enum.Enum):
    booking_created = "booking.created"
    booking_updated = "booking.updated"
    booking_cancelled = "booking.cancelled"
    payment_completed = "payment.completed"
    payment_failed = "payment.failed"
    invoice_created = "invoice.created"
    invoice_paid = "invoice.paid"
    user_created = "user.created"
    user_updated = "user.updated"

class WebhookStatus(str, enum.Enum):
    active = "active"
    inactive = "inactive"
    paused = "paused"

class WebhookDeliveryStatus(str, enum.Enum):
    pending = "pending"
    success = "success"
    failed = "failed"
    retrying = "retrying"

class Webhook(Base):
    __tablename__ = 'webhooks'
    
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    name = Column(String(255), nullable=False)
    url = Column(String(500), nullable=False)
    secret = Column(String(255), nullable=False)  # For signature verification
    
    # Event subscriptions
    events = Column(JSON, nullable=False)  # List of event types
    
    # Status
    status = Column(Enum(WebhookStatus), default=WebhookStatus.active, nullable=False, index=True)
    
    # Configuration
    retry_count = Column(Integer, default=3, nullable=False)
    timeout_seconds = Column(Integer, default=30, nullable=False)
    
    # Metadata
    description = Column(Text, nullable=True)
    created_by = Column(Integer, ForeignKey('users.id'), nullable=True)
    created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)
    
    # Relationships
    creator = relationship('User', foreign_keys=[created_by])

class WebhookDelivery(Base):
    __tablename__ = 'webhook_deliveries'
    
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    webhook_id = Column(Integer, ForeignKey('webhooks.id'), nullable=False, index=True)
    event_type = Column(String(100), nullable=False, index=True)
    event_id = Column(String(255), nullable=False, index=True)
    
    # Delivery details
    status = Column(Enum(WebhookDeliveryStatus), default=WebhookDeliveryStatus.pending, nullable=False, index=True)
    payload = Column(JSON, nullable=False)
    response_status = Column(Integer, nullable=True)
    response_body = Column(Text, nullable=True)
    error_message = Column(Text, nullable=True)
    
    # Retry information
    attempt_count = Column(Integer, default=0, nullable=False)
    next_retry_at = Column(DateTime, nullable=True)
    
    # Timestamps
    delivered_at = Column(DateTime, nullable=True)
    created_at = Column(DateTime, default=datetime.utcnow, nullable=False, index=True)
    
    # Relationships
    webhook = relationship('Webhook', foreign_keys=[webhook_id])

