"""
Linux BenchTools - Benchmark Schemas
"""

from pydantic import BaseModel, Field
from typing import Optional
from app.schemas.hardware import HardwareData


class CPUResults(BaseModel):
    """CPU benchmark results"""
    events_per_sec: Optional[float] = Field(None, ge=0)
    events_per_sec_single: Optional[float] = Field(None, ge=0)  # Monocore
    events_per_sec_multi: Optional[float] = Field(None, ge=0)  # Multicore
    duration_s: Optional[float] = Field(None, ge=0)
    score: Optional[float] = Field(None, ge=0, le=100000)
    score_single: Optional[float] = Field(None, ge=0, le=50000)  # Monocore score
    score_multi: Optional[float] = Field(None, ge=0, le=100000)  # Multicore score


class MemoryResults(BaseModel):
    """Memory benchmark results"""
    throughput_mib_s: Optional[float] = Field(None, ge=0)
    score: Optional[float] = Field(None, ge=0, le=100000)


class DiskResults(BaseModel):
    """Disk benchmark results"""
    read_mb_s: Optional[float] = Field(None, ge=0)
    write_mb_s: Optional[float] = Field(None, ge=0)
    iops_read: Optional[float] = Field(None, ge=0)
    iops_write: Optional[float] = Field(None, ge=0)
    latency_ms: Optional[float] = Field(None, ge=0)
    score: Optional[float] = Field(None, ge=0, le=50000)


class NetworkResults(BaseModel):
    """Network benchmark results"""
    upload_mbps: Optional[float] = Field(None, ge=0)
    download_mbps: Optional[float] = Field(None, ge=0)
    ping_ms: Optional[float] = Field(None, ge=0)
    jitter_ms: Optional[float] = Field(None, ge=0)
    packet_loss_percent: Optional[float] = Field(None, ge=0, le=100)
    score: Optional[float] = Field(None, ge=0, le=100000)


class GPUResults(BaseModel):
    """GPU benchmark results"""
    glmark2_score: Optional[int] = Field(None, ge=0)
    score: Optional[float] = Field(None, ge=0, le=50000)


class BenchmarkResults(BaseModel):
    """Complete benchmark results"""
    cpu: Optional[CPUResults] = None
    memory: Optional[MemoryResults] = None
    disk: Optional[DiskResults] = None
    network: Optional[NetworkResults] = None
    gpu: Optional[GPUResults] = None
    global_score: float = Field(..., ge=0, le=100000, description="Global score (weighted average of component scores)")


class BenchmarkPayload(BaseModel):
    """Complete benchmark payload from client script"""
    device_identifier: str = Field(..., min_length=1, max_length=255)
    bench_script_version: str = Field(..., min_length=1, max_length=50)
    hardware: HardwareData
    results: BenchmarkResults


class BenchmarkResponse(BaseModel):
    """Response after successful benchmark submission"""
    status: str = "ok"
    device_id: int
    benchmark_id: int
    message: Optional[str] = None


class BenchmarkDetail(BaseModel):
    """Detailed benchmark information"""
    id: int
    device_id: int
    hardware_snapshot_id: int
    run_at: str
    bench_script_version: str

    global_score: float
    cpu_score: Optional[float] = None
    cpu_score_single: Optional[float] = None
    cpu_score_multi: Optional[float] = None
    memory_score: Optional[float] = None
    disk_score: Optional[float] = None
    network_score: Optional[float] = None
    gpu_score: Optional[float] = None

    details: dict  # details_json parsed
    notes: Optional[str] = None

    class Config:
        from_attributes = True


class BenchmarkSummary(BaseModel):
    """Summary benchmark information for lists"""
    id: int
    run_at: str
    global_score: float
    cpu_score: Optional[float] = None
    cpu_score_single: Optional[float] = None
    cpu_score_multi: Optional[float] = None
    memory_score: Optional[float] = None
    disk_score: Optional[float] = None
    network_score: Optional[float] = None
    gpu_score: Optional[float] = None
    bench_script_version: Optional[str] = None
    notes: Optional[str] = None

    class Config:
        from_attributes = True


class BenchmarkUpdate(BaseModel):
    """Fields allowed when updating a benchmark"""
    notes: Optional[str] = None
