# services/production/app/schemas/equipment.py
"""
Equipment schemas for Production Service
"""

from pydantic import BaseModel, Field, ConfigDict
from typing import Optional, List
from datetime import datetime
from uuid import UUID

from app.models.production import EquipmentType, EquipmentStatus


class EquipmentCreate(BaseModel):
    """Schema for creating new equipment"""
    name: str = Field(..., min_length=1, max_length=255, description="Equipment name")
    type: EquipmentType = Field(..., description="Equipment type")
    model: Optional[str] = Field(None, max_length=100, description="Equipment model")
    serial_number: Optional[str] = Field(None, max_length=100, description="Serial number")
    location: Optional[str] = Field(None, max_length=255, description="Physical location")
    status: EquipmentStatus = Field(default=EquipmentStatus.OPERATIONAL, description="Equipment status")

    # Installation and maintenance
    install_date: Optional[datetime] = Field(None, description="Installation date")
    last_maintenance_date: Optional[datetime] = Field(None, description="Last maintenance date")
    next_maintenance_date: Optional[datetime] = Field(None, description="Next scheduled maintenance date")
    maintenance_interval_days: Optional[int] = Field(None, ge=1, description="Maintenance interval in days")

    # Performance metrics
    efficiency_percentage: Optional[float] = Field(None, ge=0, le=100, description="Current efficiency percentage")
    uptime_percentage: Optional[float] = Field(None, ge=0, le=100, description="Overall uptime percentage")
    energy_usage_kwh: Optional[float] = Field(None, ge=0, description="Current energy usage in kWh")

    # Specifications
    power_kw: Optional[float] = Field(None, ge=0, description="Power consumption in kilowatts")
    capacity: Optional[float] = Field(None, ge=0, description="Equipment capacity")
    weight_kg: Optional[float] = Field(None, ge=0, description="Weight in kilograms")

    # Temperature monitoring
    current_temperature: Optional[float] = Field(None, description="Current temperature")
    target_temperature: Optional[float] = Field(None, description="Target temperature")

    # Notes
    notes: Optional[str] = Field(None, description="Additional notes")

    model_config = ConfigDict(
        json_schema_extra={
            "example": {
                "name": "Horno Principal #1",
                "type": "oven",
                "model": "Miwe Condo CO 4.1212",
                "serial_number": "MCO-2021-001",
                "location": "Área de Horneado - Zona A",
                "status": "operational",
                "install_date": "2021-03-15T00:00:00Z",
                "maintenance_interval_days": 90,
                "efficiency_percentage": 92.0,
                "uptime_percentage": 98.5,
                "power_kw": 45.0,
                "capacity": 24.0
            }
        }
    )


class EquipmentUpdate(BaseModel):
    """Schema for updating equipment"""
    name: Optional[str] = Field(None, min_length=1, max_length=255)
    type: Optional[EquipmentType] = None
    model: Optional[str] = Field(None, max_length=100)
    serial_number: Optional[str] = Field(None, max_length=100)
    location: Optional[str] = Field(None, max_length=255)
    status: Optional[EquipmentStatus] = None

    # Installation and maintenance
    install_date: Optional[datetime] = None
    last_maintenance_date: Optional[datetime] = None
    next_maintenance_date: Optional[datetime] = None
    maintenance_interval_days: Optional[int] = Field(None, ge=1)

    # Performance metrics
    efficiency_percentage: Optional[float] = Field(None, ge=0, le=100)
    uptime_percentage: Optional[float] = Field(None, ge=0, le=100)
    energy_usage_kwh: Optional[float] = Field(None, ge=0)

    # Specifications
    power_kw: Optional[float] = Field(None, ge=0)
    capacity: Optional[float] = Field(None, ge=0)
    weight_kg: Optional[float] = Field(None, ge=0)

    # Temperature monitoring
    current_temperature: Optional[float] = None
    target_temperature: Optional[float] = None

    # Notes
    notes: Optional[str] = None

    # Status flag
    is_active: Optional[bool] = None

    model_config = ConfigDict(
        json_schema_extra={
            "example": {
                "status": "maintenance",
                "last_maintenance_date": "2024-01-15T00:00:00Z",
                "next_maintenance_date": "2024-04-15T00:00:00Z",
                "efficiency_percentage": 88.0
            }
        }
    )


class EquipmentResponse(BaseModel):
    """Schema for equipment response"""
    id: UUID
    tenant_id: UUID
    name: str
    type: EquipmentType
    model: Optional[str] = None
    serial_number: Optional[str] = None
    location: Optional[str] = None
    status: EquipmentStatus

    # Installation and maintenance
    install_date: Optional[datetime] = None
    last_maintenance_date: Optional[datetime] = None
    next_maintenance_date: Optional[datetime] = None
    maintenance_interval_days: Optional[int] = None

    # Performance metrics
    efficiency_percentage: Optional[float] = None
    uptime_percentage: Optional[float] = None
    energy_usage_kwh: Optional[float] = None

    # Specifications
    power_kw: Optional[float] = None
    capacity: Optional[float] = None
    weight_kg: Optional[float] = None

    # Temperature monitoring
    current_temperature: Optional[float] = None
    target_temperature: Optional[float] = None

    # Status
    is_active: bool
    notes: Optional[str] = None

    # Timestamps
    created_at: datetime
    updated_at: datetime

    model_config = ConfigDict(from_attributes=True)


class EquipmentListResponse(BaseModel):
    """Schema for paginated equipment list response"""
    equipment: List[EquipmentResponse]
    total_count: int
    page: int
    page_size: int

    model_config = ConfigDict(
        json_schema_extra={
            "example": {
                "equipment": [],
                "total_count": 10,
                "page": 1,
                "page_size": 50
            }
        }
    )


class EquipmentDeletionSummary(BaseModel):
    """Schema for equipment deletion summary"""
    can_delete: bool = Field(..., description="Whether the equipment can be deleted")
    warnings: List[str] = Field(default_factory=list, description="List of warnings about deletion")
    production_batches_count: int = Field(default=0, description="Number of production batches using this equipment")
    maintenance_records_count: int = Field(default=0, description="Number of maintenance records")
    temperature_logs_count: int = Field(default=0, description="Number of temperature logs")
    equipment_name: Optional[str] = Field(None, description="Equipment name")
    equipment_type: Optional[str] = Field(None, description="Equipment type")
    equipment_location: Optional[str] = Field(None, description="Equipment location")

    model_config = ConfigDict(
        json_schema_extra={
            "example": {
                "can_delete": True,
                "warnings": ["3 production batch(es) are using this equipment"],
                "production_batches_count": 3,
                "maintenance_records_count": 5,
                "temperature_logs_count": 120,
                "equipment_name": "Horno Principal #1",
                "equipment_type": "oven",
                "equipment_location": "Área de Horneado"
            }
        }
    )
