from __future__ import annotations

from datetime import datetime

from pydantic import BaseModel, Field


class PermissionRead(BaseModel):
    permission_id: int
    permission_code: str
    permission_name: str
    module_code: str
    action_code: str
    status: str


class MenuPermissionNode(BaseModel):
    permission_code: str
    permission_name: str
    children: list["MenuPermissionNode"] = Field(default_factory=list)


class OrgEmployeeNode(BaseModel):
    employee_id: int
    employee_name: str
    mobile: str | None = None
    job_title: str | None = None
    username: str | None = None
    role_names: list[str] = Field(default_factory=list)
    status: str


class OrgNodeRead(BaseModel):
    node_id: int
    node_type: str
    node_label: str
    parent_id: int | None = None
    dept_code: str
    dept_type: str
    manager_employee_id: int | None = None
    manager_name: str | None = None
    manager_employee_ids: list[int] = Field(default_factory=list)
    manager_names: list[str] = Field(default_factory=list)
    status: str
    sort_no: int = 0
    remark: str | None = None
    employees: list[OrgEmployeeNode] = Field(default_factory=list)
    children: list["OrgNodeRead"] = Field(default_factory=list)


class OrgNodeCreate(BaseModel):
    parent_id: int | None = None
    node_type: str = Field(pattern="^(COMPANY|BRANCH|DEPARTMENT|GROUP)$")
    node_label: str = Field(min_length=1, max_length=100)
    dept_code: str | None = Field(default=None, max_length=50)
    dept_type: str = Field(default="ADMIN", max_length=32)
    manager_employee_id: int | None = None
    manager_employee_ids: list[int] = Field(default_factory=list)
    sort_no: int = 0
    remark: str | None = Field(default=None, max_length=255)
    status: str = Field(default="ACTIVE", pattern="^(ACTIVE|INACTIVE)$")


class OrgNodeUpdate(BaseModel):
    node_label: str = Field(min_length=1, max_length=100)
    dept_type: str = Field(default="ADMIN", max_length=32)
    manager_employee_id: int | None = None
    manager_employee_ids: list[int] = Field(default_factory=list)
    sort_no: int = 0
    remark: str | None = Field(default=None, max_length=255)
    status: str = Field(default="ACTIVE", pattern="^(ACTIVE|INACTIVE)$")


class OrgEmployeeBind(BaseModel):
    employee_id: int = Field(gt=0)
    sort_no: int = 0


class RoleRead(BaseModel):
    role_id: int
    role_code: str
    role_name: str
    role_scope: str
    status: str
    remark: str | None = None
    permission_codes: list[str] = Field(default_factory=list)
    user_count: int = 0
    created_at: datetime | None = None
    updated_at: datetime | None = None


class RoleCreate(BaseModel):
    role_code: str = Field(min_length=2, max_length=50, pattern="^[A-Z][A-Z0-9_]*$")
    role_name: str = Field(min_length=1, max_length=100)
    role_scope: str = Field(default="SYSTEM", max_length=32)
    status: str = Field(default="ACTIVE", pattern="^(ACTIVE|INACTIVE)$")
    remark: str | None = Field(default=None, max_length=500)
    permission_codes: list[str] = Field(default_factory=list)


class RoleUpdate(BaseModel):
    role_name: str = Field(min_length=1, max_length=100)
    role_scope: str = Field(default="SYSTEM", max_length=32)
    status: str = Field(default="ACTIVE", pattern="^(ACTIVE|INACTIVE)$")
    remark: str | None = Field(default=None, max_length=500)
    permission_codes: list[str] = Field(default_factory=list)


class RolePermissionUpdate(BaseModel):
    permission_codes: list[str] = Field(default_factory=list)


class SystemUserRead(BaseModel):
    user_id: int
    username: str
    employee_id: int
    employee_name: str
    dept_id: int
    dept_name: str | None = None
    mobile: str | None = None
    nickname: str | None = None
    is_super_admin: bool = False
    status: str
    role_ids: list[int] = Field(default_factory=list)
    role_names: list[str] = Field(default_factory=list)
    last_login_at: datetime | None = None
    created_at: datetime | None = None
    updated_at: datetime | None = None


class SystemUserCreate(BaseModel):
    employee_id: int = Field(gt=0)
    role_ids: list[int] = Field(min_length=1)
    password: str = Field(min_length=6, max_length=128)
    status: str = Field(default="ACTIVE", pattern="^(ACTIVE|INACTIVE)$")


class SystemUserRoleUpdate(BaseModel):
    role_ids: list[int] = Field(min_length=1)


class SystemUserStatusUpdate(BaseModel):
    status: str = Field(pattern="^(ACTIVE|INACTIVE)$")


class SystemUserPasswordReset(BaseModel):
    password: str = Field(min_length=6, max_length=128)


class EmployeeOption(BaseModel):
    employee_id: int
    employee_name: str
    dept_id: int
    dept_name: str | None = None
    mobile: str | None = None
    job_title: str | None = None
    username: str | None = None
    role_names: list[str] = Field(default_factory=list)
    permission_codes: list[str] = Field(default_factory=list)
    has_system_user: bool = False
