from collections.abc import Callable
from functools import wraps as wraps
from typing import Any, Generic, Protocol, SupportsIndex, TypeAlias, TypeVar, overload, type_check_only

from django.db.models.base import Model
from typing_extensions import Self, override

_T = TypeVar("_T")

class cached_property(Generic[_T]):
    func: Callable[..., _T] = ...
    name: str = ...
    def __init__(self, func: Callable[..., _T], name: str = ...): ...
    @overload
    def __get__(self, instance: None, cls: type[Any] = ...) -> cached_property[_T]: ...
    @overload
    def __get__(self, instance: object, cls: type[Any] = ...) -> _T: ...

class Promise:
    def __init__(self, args: Any, kw: Any) -> None: ...
    @override
    def __reduce__(self) -> tuple[Any, tuple[Any]]: ...
    def __lt__(self, other: Any, /) -> bool: ...
    def __mod__(self, rhs: Any) -> Any: ...
    def __add__(self, other: Any) -> Any: ...
    def __radd__(self, other: Any) -> Any: ...
    def __deepcopy__(self, memo: Any) -> Self: ...

@type_check_only
class _StrPromise(Promise):
    def __add__(self, s: str, /) -> str: ...
    def __radd__(self, other: str) -> str: ...
    def __contains__(self, o: str, /) -> bool: ...
    def __ge__(self, x: str, /) -> bool: ...
    def __getitem__(self, i: SupportsIndex | slice, /) -> str: ...
    def __gt__(self, x: str, /) -> bool: ...
    def __le__(self, x: str, /) -> bool: ...
    # __len__ needed here because it defined abstract in Sequence[str]
    def __len__(self) -> int: ...
    def __lt__(self, x: str, /) -> bool: ...
    def __mod__(self, x: Any, /) -> str: ...
    def __mul__(self, n: SupportsIndex, /) -> str: ...
    def __rmul__(self, n: SupportsIndex, /) -> str: ...
    # Mypy requires this for the attribute hook to take effect
    def __getattribute__(self, name: str, /) -> Any: ...

_StrOrPromise: TypeAlias = str | _StrPromise  # noqa: PYI047

_C = TypeVar("_C", bound=Callable[..., Any])
_CS = TypeVar("_CS", bound=Callable[..., str])

def lazy(func: _C, *resultclasses: Any) -> _C: ...
def lazystr(text: Any) -> _StrPromise: ...
def keep_lazy(*resultclasses: Any) -> Callable[..., Any]: ...
def keep_lazy_text(func: _CS) -> _CS: ...

empty: object

def new_method_proxy(func: Callable[..., _T]) -> Callable[..., _T]: ...

class LazyObject:
    def __init__(self) -> None: ...
    __getattr__: Callable[..., Any] = ...
    def __setattr__(self, name: str, value: Any) -> None: ...
    def __delattr__(self, name: str) -> None: ...
    def __reduce__(self) -> tuple[Callable[..., Any], tuple[Model]]: ...
    def __copy__(self) -> LazyObject: ...
    __bytes__: Callable[..., Any] = ...
    __bool__: Callable[..., Any] = ...
    __dir__: Callable[..., Any] = ...
    __class__: Any = ...
    __ne__: Callable[..., Any] = ...
    __hash__: Callable[..., Any] = ...
    __getitem__: Callable[..., Any] = ...
    __setitem__: Callable[..., Any] = ...
    __delitem__: Callable[..., Any] = ...
    __iter__: Callable[..., Any] = ...
    __len__: Callable[..., Any] = ...
    __contains__: Callable[..., Any] = ...

def unpickle_lazyobject(wrapped: Model) -> Model: ...

class SimpleLazyObject(LazyObject):
    def __init__(self, func: Callable[[], Any]) -> None: ...
    def __copy__(self) -> SimpleLazyObject: ...

_PartitionMember = TypeVar("_PartitionMember")

def partition(
    predicate: Callable[[_PartitionMember], int | bool], values: list[_PartitionMember]
) -> tuple[list[_PartitionMember], list[_PartitionMember]]: ...

_Get = TypeVar("_Get", covariant=True)
_Self = TypeVar("_Self")

class classproperty(Generic[_Get]):
    fget: Callable[..., _Get] | None = ...
    @overload
    def __init__(self) -> None: ...
    @overload
    def __init__(self, method: Callable[[_Self], _Get]) -> None: ...
    def __get__(self, instance: _Self | None, cls: type[_Self] = ...) -> _Get: ...
    def getter(self, method: Callable[[_Self], _Get]) -> classproperty[_Get]: ...

class _Getter(Protocol[_Get]):  # noqa: PYI046
    """Type fake to declare some read-only properties (until `property` builtin is generic)

    We can use something like `Union[_Getter[str], str]` in base class to avoid errors
    when redefining attribute with property or property with attribute.
    """

    @overload
    def __get__(self: _Self, instance: None, typeobj: type[Any] | None, /) -> _Self: ...
    @overload
    def __get__(self, instance: Any, typeobj: type[Any] | None, /) -> _Get: ...
