"""
Tenant Location Schemas
"""

from pydantic import BaseModel, Field, field_validator
from typing import Optional, List, Dict, Any
from datetime import datetime
from uuid import UUID


class TenantLocationBase(BaseModel):
    """Base schema for tenant location"""
    name: str = Field(..., min_length=1, max_length=200)
    location_type: str = Field(..., pattern=r'^(central_production|retail_outlet|warehouse|store|branch)$')
    address: str = Field(..., min_length=10, max_length=500)
    city: str = Field(default="Madrid", max_length=100)
    postal_code: str = Field(..., min_length=3, max_length=10)
    latitude: Optional[float] = Field(None, ge=-90, le=90)
    longitude: Optional[float] = Field(None, ge=-180, le=180)
    contact_person: Optional[str] = Field(None, max_length=200)
    contact_phone: Optional[str] = Field(None, max_length=20)
    contact_email: Optional[str] = Field(None, max_length=255)
    is_active: bool = True
    delivery_windows: Optional[Dict[str, Any]] = None
    operational_hours: Optional[Dict[str, Any]] = None
    capacity: Optional[int] = Field(None, ge=0)
    max_delivery_radius_km: Optional[float] = Field(None, ge=0)
    delivery_schedule_config: Optional[Dict[str, Any]] = None
    metadata: Optional[Dict[str, Any]] = Field(None)


class TenantLocationCreate(TenantLocationBase):
    """Schema for creating a tenant location"""
    tenant_id: str  # This will be validated as UUID in the API layer


class TenantLocationUpdate(BaseModel):
    """Schema for updating a tenant location"""
    name: Optional[str] = Field(None, min_length=1, max_length=200)
    location_type: Optional[str] = Field(None, pattern=r'^(central_production|retail_outlet|warehouse|store|branch)$')
    address: Optional[str] = Field(None, min_length=10, max_length=500)
    city: Optional[str] = Field(None, max_length=100)
    postal_code: Optional[str] = Field(None, min_length=3, max_length=10)
    latitude: Optional[float] = Field(None, ge=-90, le=90)
    longitude: Optional[float] = Field(None, ge=-180, le=180)
    contact_person: Optional[str] = Field(None, max_length=200)
    contact_phone: Optional[str] = Field(None, max_length=20)
    contact_email: Optional[str] = Field(None, max_length=255)
    is_active: Optional[bool] = None
    delivery_windows: Optional[Dict[str, Any]] = None
    operational_hours: Optional[Dict[str, Any]] = None
    capacity: Optional[int] = Field(None, ge=0)
    max_delivery_radius_km: Optional[float] = Field(None, ge=0)
    delivery_schedule_config: Optional[Dict[str, Any]] = None
    metadata: Optional[Dict[str, Any]] = Field(None)


class TenantLocationResponse(TenantLocationBase):
    """Schema for tenant location response"""
    id: str
    tenant_id: str
    created_at: datetime
    updated_at: Optional[datetime]

    @field_validator('id', 'tenant_id', mode='before')
    @classmethod
    def convert_uuid_to_string(cls, v):
        """Convert UUID objects to strings for JSON serialization"""
        if isinstance(v, UUID):
            return str(v)
        return v

    class Config:
        from_attributes = True
        populate_by_name = True


class TenantLocationsResponse(BaseModel):
    """Schema for multiple tenant locations response"""
    locations: List[TenantLocationResponse]
    total: int


class TenantLocationTypeFilter(BaseModel):
    """Schema for filtering locations by type"""
    location_types: List[str] = Field(
        default=["central_production", "retail_outlet", "warehouse", "store", "branch"],
        description="List of location types to include"
    )