from __future__ import annotations

from datetime import datetime

from pydantic import BaseModel, Field

from ...database.orm import SalaryProfileORM
from .employee import EmployeeResponse


class SalaryProfileRequest(BaseModel):
    """薪资档案维护请求。"""

    employee_id: int
    salary_mode: str = Field(default="monthly", max_length=32)
    base_salary: float = Field(default=0, ge=0)
    hourly_rate: float = Field(default=0, ge=0)
    piece_quantity: float = Field(default=0, ge=0)
    piece_unit_price: float = Field(default=0, ge=0)
    probation_type: str = Field(default="ratio", max_length=32)
    probation_ratio: float = Field(default=1, ge=0)
    probation_salary: float = Field(default=0, ge=0)
    commission_amount: float = Field(default=0, ge=0)
    overtime_rate: float = Field(default=0, ge=0)
    weekend_overtime_rate: float = Field(default=0, ge=0)
    holiday_overtime_rate: float = Field(default=0, ge=0)
    effective_month: str = Field(default="", max_length=7)
    remark: str = ""


class SalaryProfileResponse(BaseModel):
    """薪资档案响应。"""

    id: int
    employee_id: int
    employee: EmployeeResponse
    salary_mode: str
    base_salary: float
    hourly_rate: float
    piece_quantity: float
    piece_unit_price: float
    probation_type: str
    probation_ratio: float
    probation_salary: float
    commission_amount: float
    overtime_rate: float
    weekend_overtime_rate: float
    holiday_overtime_rate: float
    effective_month: str
    remark: str
    created_at: datetime
    updated_at: datetime

    @classmethod
    def from_orm_model(cls, profile: SalaryProfileORM) -> "SalaryProfileResponse":
        return cls(
            id=profile.id,
            employee_id=profile.employee_id,
            employee=EmployeeResponse.from_orm_model(profile.employee),
            salary_mode=profile.salary_mode,
            base_salary=profile.base_salary,
            hourly_rate=profile.hourly_rate,
            piece_quantity=profile.piece_quantity,
            piece_unit_price=profile.piece_unit_price,
            probation_type=profile.probation_type,
            probation_ratio=profile.probation_ratio,
            probation_salary=profile.probation_salary,
            commission_amount=profile.commission_amount,
            overtime_rate=profile.overtime_rate,
            weekend_overtime_rate=profile.weekend_overtime_rate,
            holiday_overtime_rate=profile.holiday_overtime_rate,
            effective_month=profile.effective_month,
            remark=profile.remark,
            created_at=profile.created_at,
            updated_at=profile.updated_at,
        )
