"""Asset InMemory Repository 实现"""

from packages.domain import Asset


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

    def __init__(self):
        self._assets: dict[str, Asset] = {}

    def create(self, asset: Asset) -> Asset:
        self._assets[asset.id] = asset
        return asset

    def get(self, asset_id: str) -> Asset | None:
        return self._assets.get(asset_id)

    def list_by_project(self, project_id: str) -> list[Asset]:
        return [asset for asset in self._assets.values() if asset.project_id == project_id]

    def list_by_library(self, library_id: str) -> list[Asset]:
        return [asset for asset in self._assets.values() if asset.library_id == library_id]

    def find_by_library(self, library_id: str) -> list[Asset]:
        """Alias for list_by_library to match the port interface."""
        return self.list_by_library(library_id)

    def find_by_library_and_file_type(self, library_id: str, file_type: str) -> list[Asset]:
        return [
            asset
            for asset in self._assets.values()
            if asset.library_id == library_id and asset.mime_type and asset.mime_type.startswith(file_type)
        ]

    def update(self, asset: Asset) -> Asset:
        self._assets[asset.id] = asset
        return asset

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

    def batch_delete(self, asset_ids: list[str]) -> int:
        """批量删除素材，返回实际删除数量。"""
        count = 0
        for aid in asset_ids:
            if aid in self._assets:
                del self._assets[aid]
                count += 1
        return count

    def find_by_project(
        self,
        project_id: str,
        skip: int = 0,
        limit: int = 100,
    ) -> list[Asset]:
        items = [a for a in self._assets.values() if a.project_id == project_id]
        return items[skip : skip + limit]

    def find_by_id(self, asset_id: str) -> Asset | None:
        return self._assets.get(asset_id)

    def find_by_tag_ids(
        self,
        tag_ids: list[str],
        skip: int = 0,
        limit: int = 100,
    ) -> list[Asset]:
        """查找包含所有指定标签的素材。"""
        if not tag_ids:
            return []
        tag_set = set(tag_ids)
        items = [a for a in self._assets.values() if tag_set.issubset(set(a.tag_ids))]
        return items[skip : skip + limit]

    def find_by_library_and_file_hash(
        self,
        library_id: str,
        file_hash: str,
    ) -> Asset | None:
        """按素材库 + 文件哈希查找已有素材（去重检测）。"""
        if not file_hash:
            return None
        for asset in self._assets.values():
            if asset.library_id == library_id and asset.file_hash == file_hash:
                return asset
        return None
