from pydantic import BaseModel
from typing import List, Optional

# ========== 项目与指标模型 ==========
class ProjectCreate(BaseModel):
    project_name: str
    eval_mode: str

class MetricCreate(BaseModel):
    category_level_1: str
    category_level_2: str
    metric_name: str
    value_type: str
    description: str = ""

# ========== 资产与配置模型 ==========
class AssetCreate(BaseModel):
    project_id: Optional[int] = None
    asset_category: str 
    asset_name: str
    asset_type: str
    asset_code: str

# 绑定指标时包含上限和单位
class BoundMetricItem(BaseModel):
    metric_id: int
    upper_limit: Optional[float] = None
    lower_limit: Optional[float] = None
    baseline_value: Optional[float] = None
    failure_value: Optional[float] = None
    ideal_value: Optional[float] = None
    extreme_value: Optional[float] = None
    criterion_weight: Optional[float] = None
    indicator_weight: Optional[float] = None
    cycle_hours: Optional[float] = None
    unit: Optional[str] = None

class BindMetrics(BaseModel):
    metrics: List[BoundMetricItem]

# ========== 录入测试数据模型 ==========
class RecordItem(BaseModel):
    metric_id: int
    raw_data: dict

class RecordCreate(BaseModel):
    asset_id: int
    record_date: str  # 接收前端传来的历史补录时间
    records: List[RecordItem]


class ProductCreate(BaseModel):
    product_code: str
    product_name: str
    product_detail: str = ""


class AssetProductBindItem(BaseModel):
    product_id: int
    total_quantity: Optional[int] = None
    process_note: Optional[str] = ""


class AssetProductBindBatch(BaseModel):
    bindings: List[AssetProductBindItem]


class ProductMetricConfigItem(BaseModel):
    metric_id: int
    upper_limit: Optional[float] = None
    lower_limit: Optional[float] = None
    ideal_value: Optional[float] = None
    extreme_value: Optional[float] = None
    unit: Optional[str] = None
    position_note: Optional[str] = ""


class ProductMetricConfigBatch(BaseModel):
    metrics: List[ProductMetricConfigItem]


class ProductSampleMetricItem(BaseModel):
    metric_id: int
    values: List[float]


class ProductRecordCreate(BaseModel):
    asset_id: int
    product_id: int
    batch_no: str
    record_date: str
    production_quantity: Optional[int] = None
    remark: Optional[str] = ""
    metric_samples: List[ProductSampleMetricItem]


class UserCreate(BaseModel):
    username: str
    real_name: Optional[str] = ""
    role: str = "user"
    email: Optional[str] = ""
    is_active: bool = True
    password: str


class UserUpdate(BaseModel):
    username: str
    real_name: Optional[str] = ""
    role: str = "user"
    email: Optional[str] = ""
    is_active: bool = True
    password: Optional[str] = None


class UserLogin(BaseModel):
    username: str
    password: str
