"""
員工身份 Schemas
"""
from datetime import date, datetime
from typing import Optional
from pydantic import Field, ConfigDict

from app.schemas.base import BaseSchema, TimestampSchema


class EmployeeIdentityBase(BaseSchema):
    """員工身份基礎 Schema"""

    job_title: str = Field(..., min_length=2, max_length=100, description="職稱")
    job_level: str = Field(..., description="職級 (Junior/Mid/Senior/Manager)")
    email_quota_mb: int = Field(..., gt=0, description="郵件配額 (MB)")


class EmployeeIdentityCreate(EmployeeIdentityBase):
    """創建員工身份 Schema"""

    employee_id: int = Field(..., description="員工 ID")
    business_unit_id: int = Field(..., description="事業部 ID")
    department_id: Optional[int] = Field(None, description="部門 ID")
    is_primary: bool = Field(False, description="是否為主要身份")
    started_at: date = Field(..., description="開始日期")

    model_config = ConfigDict(
        json_schema_extra={
            "example": {
                "employee_id": 1,
                "business_unit_id": 2,
                "department_id": 4,
                "job_title": "技術總監",
                "job_level": "Senior",
                "email_quota_mb": 5000,
                "is_primary": True,
                "started_at": "2020-01-01"
            }
        }
    )


class EmployeeIdentityUpdate(BaseSchema):
    """更新員工身份 Schema"""

    department_id: Optional[int] = None
    job_title: Optional[str] = Field(None, min_length=2, max_length=100)
    job_level: Optional[str] = None
    email_quota_mb: Optional[int] = Field(None, gt=0)
    is_primary: Optional[bool] = None
    ended_at: Optional[date] = None
    is_active: Optional[bool] = None


class EmployeeIdentityInDB(EmployeeIdentityBase, TimestampSchema):
    """資料庫中的員工身份 Schema"""

    id: int
    employee_id: int
    username: str = Field(..., description="SSO 帳號")
    keycloak_id: str = Field(..., description="Keycloak UUID")
    business_unit_id: int
    department_id: Optional[int] = None
    is_primary: bool
    started_at: date
    ended_at: Optional[date] = None
    is_active: bool

    model_config = ConfigDict(from_attributes=True)


class EmployeeIdentityResponse(EmployeeIdentityInDB):
    """員工身份響應 Schema"""

    employee_name: Optional[str] = Field(None, description="員工姓名")
    business_unit_name: Optional[str] = Field(None, description="事業部名稱")
    department_name: Optional[str] = Field(None, description="部門名稱")
    email_domain: Optional[str] = Field(None, description="郵件網域")

    model_config = ConfigDict(
        json_schema_extra={
            "example": {
                "id": 1,
                "employee_id": 1,
                "username": "porsche.chen@lab.taipei",
                "keycloak_id": "abc123-uuid",
                "business_unit_id": 2,
                "department_id": 4,
                "job_title": "技術總監",
                "job_level": "Senior",
                "email_quota_mb": 5000,
                "is_primary": True,
                "started_at": "2020-01-01",
                "ended_at": None,
                "is_active": True,
                "created_at": "2020-01-01T00:00:00",
                "updated_at": "2020-01-01T00:00:00",
                "employee_name": "陳保時",
                "business_unit_name": "智能發展部",
                "department_name": "資訊部",
                "email_domain": "lab.taipei"
            }
        }
    )


class EmployeeIdentityListItem(BaseSchema):
    """員工身份列表項 Schema"""

    id: int
    username: str
    job_title: str
    job_level: str
    is_primary: bool
    is_active: bool

    model_config = ConfigDict(from_attributes=True)
