# @generated
# This file was automatically generated by the ncae-sdk code generator.
# DO NOT EDIT MANUALLY, ANY CHANGES WILL BE OVERWRITTEN.

from typing import Any, Final, Iterator, Optional

from typing_extensions import Unpack

from ncae_sdk._resource import ResourceId
from ncae_sdk._sync.endpoint import Endpoint
from ncae_sdk._util import map_sync
from ncae_sdk.resources._schema import (
    User,
    UserCreate,
    UserCreateModel,
    UserFilter,
    UserFilterModel,
    UserUpdate,
    UserUpdateModel,
)


class UserEndpoint(Endpoint):
    BASE_PATH: Final[str] = "admin/v1/user"

    def list(self, **filters: Unpack[UserFilter]) -> Iterator[User]:
        results = self._list(self.BASE_PATH, UserFilterModel, filters)
        return map_sync(User.parse_api, results)

    def find(self, **filters: Unpack[UserFilter]) -> Optional[User]:
        result = self._get_by_filters(self.BASE_PATH, UserFilterModel, filters)
        return User.parse_api(result) if result else None

    def get(self, rid: ResourceId) -> Optional[User]:
        result = self._get_by_id(self.BASE_PATH, rid)
        return User.parse_api(result) if result else None

    def create(self, **payload: Unpack[UserCreate]) -> User:
        result = self._create(self.BASE_PATH, UserCreateModel, payload, preprocessor=self._preprocessor)
        return User.parse_api(result)

    def update(self, rid: ResourceId, /, **payload: Unpack[UserUpdate]) -> User:
        result = self._update(self.BASE_PATH, rid, UserUpdateModel, payload, preprocessor=self._preprocessor)
        return User.parse_api(result)

    def set_password(self, rid: ResourceId, password: str) -> None:
        self._session.post(
            f"{self.BASE_PATH}/{rid}/set_password",
            json={"password": password, "repeat_password": password},
        )

    @staticmethod
    def _preprocessor(value: dict[str, Any]) -> dict[str, Any]:
        if password := value.get("password"):
            value["repeat_password"] = password
        if become_password := value.get("become_password"):
            value["repeat_become_password"] = become_password

        return value
