from __future__ import annotations

from datetime import datetime

from pydantic import BaseModel, Field

from ...database.orm import DepartmentORM, PositionORM


class DepartmentRequest(BaseModel):
    """部门维护请求。"""

    parent_id: int | None = None
    department_code: str = Field(..., max_length=64)
    name: str = Field(..., max_length=128)
    sort_order: int = 0
    is_active: bool = True
    remark: str = ""


class DepartmentResponse(BaseModel):
    """部门响应。"""

    id: int
    parent_id: int | None
    department_code: str
    name: str
    sort_order: int
    is_active: bool
    remark: str
    created_at: datetime
    updated_at: datetime

    @classmethod
    def from_orm_model(cls, department: DepartmentORM) -> "DepartmentResponse":
        return cls(
            id=department.id,
            parent_id=department.parent_id,
            department_code=department.department_code,
            name=department.name,
            sort_order=department.sort_order,
            is_active=department.is_active,
            remark=department.remark,
            created_at=department.created_at,
            updated_at=department.updated_at,
        )


class PositionRequest(BaseModel):
    """岗位维护请求。"""

    department_id: int
    position_code: str = Field(default="", max_length=64)
    name: str = Field(..., max_length=128)
    sort_order: int = 0
    is_active: bool = True
    remark: str = ""


class PositionResponse(BaseModel):
    """岗位响应。"""

    id: int
    department_id: int
    department: DepartmentResponse
    position_code: str
    name: str
    sort_order: int
    is_active: bool
    remark: str
    created_at: datetime
    updated_at: datetime

    @classmethod
    def from_orm_model(cls, position: PositionORM) -> "PositionResponse":
        return cls(
            id=position.id,
            department_id=position.department_id,
            department=DepartmentResponse.from_orm_model(position.department),
            position_code=position.position_code,
            name=position.name,
            sort_order=position.sort_order,
            is_active=position.is_active,
            remark=position.remark,
            created_at=position.created_at,
            updated_at=position.updated_at,
        )
