from pydantic import BaseModel
from typing import Optional, List
from datetime import datetime


class QcTaskCreate(BaseModel):
    task_code: str
    coil_no: Optional[str] = None
    task_type: Optional[str] = None
    scheme_name: Optional[str] = None
    inspect_user: Optional[str] = None
    remark: Optional[str] = None


class QcTaskUpdate(BaseModel):
    coil_no: Optional[str] = None
    task_type: Optional[str] = None
    scheme_name: Optional[str] = None
    status: Optional[int] = None
    inspect_user: Optional[str] = None
    inspect_time: Optional[datetime] = None
    audit_user: Optional[str] = None
    audit_time: Optional[datetime] = None
    result: Optional[str] = None
    remark: Optional[str] = None


class QcTaskItemOut(BaseModel):
    id: int
    task_id: int
    item_name: str
    item_type: Optional[str]
    standard_value: Optional[float]
    upper_limit: Optional[float]
    lower_limit: Optional[float]
    unit: Optional[str]
    inspect_value: Optional[str]
    is_qualified: Optional[int]
    judge_result: Optional[str]
    inspect_user: Optional[str]
    inspect_time: Optional[datetime]
    created_at: datetime
    class Config:
        from_attributes = True


class QcTaskItemCreate(BaseModel):
    task_id: int
    item_name: str
    item_type: Optional[str] = None
    standard_value: Optional[float] = None
    upper_limit: Optional[float] = None
    lower_limit: Optional[float] = None
    unit: Optional[str] = None
    inspect_value: Optional[str] = None
    is_qualified: Optional[int] = None
    judge_result: Optional[str] = None
    inspect_user: Optional[str] = None
    inspect_time: Optional[datetime] = None


class QcTaskItemUpdate(BaseModel):
    inspect_value: Optional[str] = None
    is_qualified: Optional[int] = None
    judge_result: Optional[str] = None
    inspect_user: Optional[str] = None
    inspect_time: Optional[datetime] = None


class QcTaskOut(BaseModel):
    id: int
    task_code: str
    coil_no: Optional[str]
    task_type: Optional[str]
    scheme_name: Optional[str]
    status: int
    inspect_user: Optional[str]
    inspect_time: Optional[datetime]
    audit_user: Optional[str]
    audit_time: Optional[datetime]
    result: Optional[str]
    remark: Optional[str]
    created_at: datetime
    class Config:
        from_attributes = True


class QcDefectBase(BaseModel):
    coil_no: Optional[str] = None
    seq_no: Optional[int] = None
    defect_desc: Optional[str] = None
    start_position: Optional[float] = None
    end_position: Optional[float] = None
    length_val: Optional[float] = None
    upper_surface: Optional[bool] = False
    lower_surface: Optional[bool] = False
    side_op: Optional[bool] = False
    side_middle: Optional[bool] = False
    side_drive: Optional[bool] = False
    is_main: Optional[bool] = False
    inherit_source: Optional[str] = None
    image_url: Optional[str] = None
    defect_code: Optional[str] = None
    defect_type: Optional[str] = None
    degree: Optional[str] = None
    remark: Optional[str] = None


class QcDefectCreate(QcDefectBase):
    production_line: Optional[str] = None
    position: Optional[str] = None
    plate_surface: Optional[str] = None
    defect_rate: Optional[float] = None
    defect_weight: Optional[float] = None
    judge_level: Optional[str] = None
    judge_by: Optional[str] = None
    judge_time: Optional[datetime] = None
    main_mark: Optional[int] = None
    whole_coil_mark: Optional[int] = None


class QcDefectUpdate(BaseModel):
    seq_no: Optional[int] = None
    defect_desc: Optional[str] = None
    start_position: Optional[float] = None
    end_position: Optional[float] = None
    length_val: Optional[float] = None
    upper_surface: Optional[bool] = None
    lower_surface: Optional[bool] = None
    side_op: Optional[bool] = None
    side_middle: Optional[bool] = None
    side_drive: Optional[bool] = None
    is_main: Optional[bool] = None
    inherit_source: Optional[str] = None
    image_url: Optional[str] = None
    defect_code: Optional[str] = None
    defect_type: Optional[str] = None
    degree: Optional[str] = None
    remark: Optional[str] = None


class QcDefectBulkSave(BaseModel):
    coil_no: str
    defects: List[QcDefectCreate]


class QcDefectOut(BaseModel):
    id: int
    coil_no: Optional[str]
    seq_no: Optional[int] = None
    defect_desc: Optional[str] = None
    start_position: Optional[float] = None
    end_position: Optional[float] = None
    length_val: Optional[float] = None
    upper_surface: Optional[bool] = None
    lower_surface: Optional[bool] = None
    side_op: Optional[bool] = None
    side_middle: Optional[bool] = None
    side_drive: Optional[bool] = None
    is_main: Optional[bool] = None
    inherit_source: Optional[str] = None
    image_url: Optional[str] = None
    production_line: Optional[str] = None
    position: Optional[str] = None
    plate_surface: Optional[str] = None
    defect_code: Optional[str] = None
    defect_type: Optional[str] = None
    defect_rate: Optional[float] = None
    defect_weight: Optional[float] = None
    degree: Optional[str] = None
    judge_level: Optional[str] = None
    judge_by: Optional[str] = None
    judge_time: Optional[datetime] = None
    main_mark: Optional[int] = None
    whole_coil_mark: Optional[int] = None
    remark: Optional[str] = None
    created_at: datetime
    class Config:
        from_attributes = True
