"""
郵件帳號 Schemas
"""
from datetime import datetime
from typing import Optional
from pydantic import BaseModel, Field, EmailStr, ConfigDict, field_validator

from app.schemas.base import BaseSchema, TimestampSchema


class EmailAccountBase(BaseSchema):
    """郵件帳號基礎 Schema"""

    email_address: EmailStr = Field(..., description="郵件地址")
    quota_mb: int = Field(2048, ge=1024, le=102400, description="配額 (MB), 1GB-100GB")
    forward_to: Optional[EmailStr] = Field(None, description="轉寄地址")
    auto_reply: Optional[str] = Field(None, max_length=1000, description="自動回覆內容")
    is_active: bool = Field(True, description="是否啟用")


class EmailAccountCreate(EmailAccountBase):
    """創建郵件帳號 Schema"""

    employee_id: int = Field(..., description="員工 ID")

    model_config = ConfigDict(
        json_schema_extra={
            "example": {
                "employee_id": 1,
                "email_address": "porsche.chen@porscheworld.tw",
                "quota_mb": 2048,
                "forward_to": None,
                "auto_reply": None,
                "is_active": True
            }
        }
    )


class EmailAccountUpdate(BaseSchema):
    """更新郵件帳號 Schema"""

    quota_mb: Optional[int] = Field(None, ge=1024, le=102400, description="配額 (MB)")
    forward_to: Optional[EmailStr] = Field(None, description="轉寄地址")
    auto_reply: Optional[str] = Field(None, max_length=1000, description="自動回覆內容")
    is_active: Optional[bool] = Field(None, description="是否啟用")

    @field_validator('forward_to')
    @classmethod
    def validate_forward_to(cls, v):
        """允許空字串來清除轉寄地址"""
        if v == "":
            return None
        return v

    @field_validator('auto_reply')
    @classmethod
    def validate_auto_reply(cls, v):
        """允許空字串來清除自動回覆"""
        if v == "":
            return None
        return v


class EmailAccountInDB(EmailAccountBase, TimestampSchema):
    """資料庫中的郵件帳號 Schema"""

    id: int
    tenant_id: int
    employee_id: int

    model_config = ConfigDict(from_attributes=True)


class EmailAccountResponse(EmailAccountInDB):
    """郵件帳號響應 Schema (包含關聯資料)"""

    employee_name: Optional[str] = Field(None, description="員工姓名")
    employee_number: Optional[str] = Field(None, description="員工編號")

    model_config = ConfigDict(
        json_schema_extra={
            "example": {
                "id": 1,
                "tenant_id": 1,
                "employee_id": 1,
                "email_address": "porsche.chen@porscheworld.tw",
                "quota_mb": 2048,
                "forward_to": None,
                "auto_reply": None,
                "is_active": True,
                "employee_name": "陳保時",
                "employee_number": "EMP001",
                "created_at": "2020-01-01T00:00:00",
                "updated_at": "2020-01-01T00:00:00"
            }
        }
    )


class EmailAccountListItem(BaseSchema):
    """郵件帳號列表項 Schema (簡化版)"""

    id: int
    email_address: str
    quota_mb: int
    is_active: bool
    employee_id: int
    employee_name: Optional[str] = None
    employee_number: Optional[str] = None

    model_config = ConfigDict(from_attributes=True)


class EmailAccountQuotaUpdate(BaseSchema):
    """郵件配額更新 Schema"""

    quota_mb: int = Field(..., ge=1024, le=102400, description="配額 (MB), 1GB-100GB")

    model_config = ConfigDict(
        json_schema_extra={
            "example": {
                "quota_mb": 5120  # 5GB
            }
        }
    )
