"""AssetLibrary InMemory Repository 实现"""

from packages.domain import AssetLibrary, AssetLibraryKind


class InMemoryAssetLibraryRepository:
    """素材库 InMemory 仓储实现（用于同步用例和测试）"""

    def __init__(self):
        self._libraries: dict[str, AssetLibrary] = {}

    def create(self, library: AssetLibrary) -> AssetLibrary:
        self._libraries[library.id] = library
        return library

    def get(self, library_id: str) -> AssetLibrary | None:
        return self._libraries.get(library_id)

    def find_by_project(self, project_id: str, kind: AssetLibraryKind | None = None) -> list[AssetLibrary]:
        items = [library for library in self._libraries.values() if library.project_id == project_id]
        if kind is not None:
            items = [library for library in items if library.kind == kind]
        return items

    def update(self, library: AssetLibrary) -> AssetLibrary:
        self._libraries[library.id] = library
        return library

    def delete(self, library_id: str) -> bool:
        if library_id in self._libraries:
            del self._libraries[library_id]
            return True
        return False

    def increment_asset_count(self, library_id: str, size_delta: int) -> None:
        library = self._libraries.get(library_id)
        if library:
            library.asset_count += 1
            library.total_size += size_delta

    def decrement_asset_count(self, library_id: str, size_delta: int) -> None:
        library = self._libraries.get(library_id)
        if library:
            library.asset_count = max(0, library.asset_count - 1)
            library.total_size = max(0, library.total_size - size_delta)
