"""
Pydantic schemas for POS configuration API requests and responses
"""

from typing import Optional, List, Dict, Any
from datetime import datetime
from pydantic import BaseModel, Field
from enum import Enum


class POSProvider(str, Enum):
    """POS provider types"""
    SQUARE = "square"
    TOAST = "toast"
    LIGHTSPEED = "lightspeed"


class POSConfigurationBase(BaseModel):
    """Base schema for POS configurations"""
    
    class Config:
        from_attributes = True
        use_enum_values = True
        json_encoders = {
            datetime: lambda v: v.isoformat() if v else None
        }


class POSConfigurationResponse(POSConfigurationBase):
    """Schema for POS configuration API responses"""
    id: str
    tenant_id: str
    pos_system: POSProvider
    provider_name: str
    is_active: bool
    is_connected: bool
    webhook_url: Optional[str] = None
    webhook_secret: Optional[str] = None
    environment: str = "sandbox"
    location_id: Optional[str] = None
    merchant_id: Optional[str] = None
    sync_enabled: bool = True
    sync_interval_minutes: str = "5"
    auto_sync_products: bool = True
    auto_sync_transactions: bool = True
    last_sync_at: Optional[datetime] = None
    last_successful_sync_at: Optional[datetime] = None
    last_sync_status: Optional[str] = None
    last_sync_message: Optional[str] = None
    provider_settings: Optional[Dict[str, Any]] = None
    last_health_check_at: Optional[datetime] = None
    health_status: str = "unknown"
    health_message: Optional[str] = None
    created_at: datetime
    updated_at: datetime
    notes: Optional[str] = None

    @classmethod
    def from_orm(cls, obj):
        """Convert ORM object to schema with proper UUID handling"""
        return cls(
            id=str(obj.id),
            tenant_id=str(obj.tenant_id),
            pos_system=obj.pos_system,
            provider_name=obj.provider_name,
            is_active=obj.is_active,
            is_connected=obj.is_connected,
            webhook_url=obj.webhook_url,
            webhook_secret=obj.webhook_secret,
            environment=obj.environment,
            location_id=obj.location_id,
            merchant_id=obj.merchant_id,
            sync_enabled=obj.sync_enabled,
            sync_interval_minutes=obj.sync_interval_minutes,
            auto_sync_products=obj.auto_sync_products,
            auto_sync_transactions=obj.auto_sync_transactions,
            last_sync_at=obj.last_sync_at,
            last_successful_sync_at=obj.last_successful_sync_at,
            last_sync_status=obj.last_sync_status,
            last_sync_message=obj.last_sync_message,
            provider_settings=obj.provider_settings,
            last_health_check_at=obj.last_health_check_at,
            health_status=obj.health_status,
            health_message=obj.health_message,
            created_at=obj.created_at,
            updated_at=obj.updated_at,
            notes=obj.notes
        )


class POSConfigurationListResponse(BaseModel):
    """Schema for POS configuration list API response"""
    configurations: List[POSConfigurationResponse]
    total: int
    supported_systems: List[str] = ["square", "toast", "lightspeed"]
