"""
事業部 Schemas
"""
from datetime import datetime
from typing import Optional
from pydantic import Field, ConfigDict

from app.schemas.base import BaseSchema


class BusinessUnitBase(BaseSchema):
    """事業部基礎 Schema"""

    name: str = Field(..., min_length=2, max_length=100, description="事業部名稱")
    name_en: Optional[str] = Field(None, max_length=100, description="英文名稱")
    code: str = Field(..., min_length=2, max_length=20, description="事業部代碼")
    email_domain: str = Field(..., description="郵件網域")
    description: Optional[str] = Field(None, description="說明")


class BusinessUnitCreate(BusinessUnitBase):
    """創建事業部 Schema"""

    model_config = ConfigDict(
        json_schema_extra={
            "example": {
                "name": "業務發展部",
                "name_en": "Business Development",
                "code": "biz",
                "email_domain": "ease.taipei",
                "description": "碳權申請諮詢、碳足跡盤查、碳權交易媒合、業務拓展"
            }
        }
    )


class BusinessUnitUpdate(BaseSchema):
    """更新事業部 Schema"""

    name: Optional[str] = Field(None, min_length=2, max_length=100)
    name_en: Optional[str] = Field(None, max_length=100)
    description: Optional[str] = None
    is_active: Optional[bool] = None


class BusinessUnitInDB(BusinessUnitBase):
    """資料庫中的事業部 Schema"""

    id: int
    is_active: bool
    created_at: datetime

    model_config = ConfigDict(from_attributes=True)


class BusinessUnitResponse(BusinessUnitInDB):
    """事業部響應 Schema"""

    departments_count: Optional[int] = Field(None, description="部門數量")
    employees_count: Optional[int] = Field(None, description="員工數量")

    model_config = ConfigDict(
        json_schema_extra={
            "example": {
                "id": 1,
                "name": "業務發展部",
                "name_en": "Business Development",
                "code": "biz",
                "email_domain": "ease.taipei",
                "description": "碳權申請諮詢、碳足跡盤查、碳權交易媒合、業務拓展",
                "is_active": True,
                "created_at": "2020-01-01T00:00:00",
                "departments_count": 3,
                "employees_count": 15
            }
        }
    )


class BusinessUnitListItem(BaseSchema):
    """事業部列表項 Schema"""

    id: int
    name: str
    code: str
    email_domain: str
    is_active: bool

    model_config = ConfigDict(from_attributes=True)
