# services/pos/app/models/pos_config.py
"""
POS Configuration Model
Stores POS system configurations for each tenant
"""

from sqlalchemy import Column, String, DateTime, Boolean, Text, JSON, Index
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.sql import func
import uuid

from shared.database.base import Base


class POSConfiguration(Base):
    """
    POS system configuration for tenants
    Stores encrypted credentials and settings for each POS provider
    """
    __tablename__ = "pos_configurations"
    
    # Primary identifiers
    id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4, index=True)
    tenant_id = Column(UUID(as_uuid=True), nullable=False, index=True)
    
    # POS Provider Information
    pos_system = Column(String(50), nullable=False)  # square, toast, lightspeed
    provider_name = Column(String(100), nullable=False)  # Display name for the provider
    
    # Configuration Status
    is_active = Column(Boolean, default=True, nullable=False)
    is_connected = Column(Boolean, default=False, nullable=False)
    
    # Authentication & Credentials (encrypted)
    encrypted_credentials = Column(Text, nullable=True)  # JSON with encrypted API keys/tokens
    webhook_url = Column(String(500), nullable=True)
    webhook_secret = Column(String(255), nullable=True)
    
    # Provider-specific Settings
    environment = Column(String(20), default="sandbox", nullable=False)  # sandbox, production
    location_id = Column(String(100), nullable=True)  # For multi-location setups
    merchant_id = Column(String(100), nullable=True)  # Provider merchant ID
    
    # Sync Configuration
    sync_enabled = Column(Boolean, default=True, nullable=False)
    sync_interval_minutes = Column(String(10), default="5", nullable=False)
    auto_sync_products = Column(Boolean, default=True, nullable=False)
    auto_sync_transactions = Column(Boolean, default=True, nullable=False)
    
    # Last Sync Information
    last_sync_at = Column(DateTime(timezone=True), nullable=True)
    last_successful_sync_at = Column(DateTime(timezone=True), nullable=True)
    last_sync_status = Column(String(50), nullable=True)  # success, failed, partial
    last_sync_message = Column(Text, nullable=True)
    
    # Provider-specific Configuration (JSON)
    provider_settings = Column(JSON, nullable=True)
    
    # Connection Health
    last_health_check_at = Column(DateTime(timezone=True), nullable=True)
    health_status = Column(String(50), default="unknown", nullable=False)  # healthy, unhealthy, unknown
    health_message = Column(Text, nullable=True)
    
    # Timestamps
    created_at = Column(DateTime(timezone=True), server_default=func.now(), nullable=False)
    updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), nullable=False)
    
    # Metadata
    created_by = Column(UUID(as_uuid=True), nullable=True)
    notes = Column(Text, nullable=True)
    
    # Indexes for performance
    __table_args__ = (
        Index('idx_pos_config_tenant_pos_system', 'tenant_id', 'pos_system'),
        Index('idx_pos_config_active', 'is_active'),
        Index('idx_pos_config_connected', 'is_connected'),
        Index('idx_pos_config_sync_enabled', 'sync_enabled'),
        Index('idx_pos_config_health_status', 'health_status'),
        Index('idx_pos_config_created_at', 'created_at'),
    )
    
    def __repr__(self):
        return f"<POSConfiguration(id={self.id}, tenant_id={self.tenant_id}, pos_system='{self.pos_system}', is_active={self.is_active})>"