# src/chitai/controllers/tag.py

# Standard library
from typing import Annotated

# Third-party libraries
from litestar import Controller, post, get, patch, delete
from litestar.params import Dependency
from advanced_alchemy.extensions.litestar.providers import create_service_dependencies
from advanced_alchemy.service.pagination import OffsetPagination
from advanced_alchemy.service import FilterTypeT

# Local imports
from chitai.services import TagService
from chitai.schemas import TagRead
from chitai.services.filters.tags import TagLibraryFilter


class TagController(Controller):
    """Controller for managing tag information."""

    path = "/tags"
    dependencies = create_service_dependencies(
        TagService,
        key="tag_service",
        filters={
            "id_filter": int,
            "pagination_type": "limit_offset",
            "sort_field": "name",
            "search": ["name"],
            "search_ignore_case": True,
        },
    )

    @get()
    async def list_tags(
        self,
        tag_service: TagService,
        libraries: list[int] | None = None,
        filters: Annotated[list[FilterTypeT], Dependency(skip_validation=True)] = [],
    ) -> OffsetPagination[TagRead]:
        """
        List tags with filtering, pagination, and search.

        Supports searching by name and filtering by library. Results can be sorted
        and paginated using standard query parameters.

        Query Parameters:
            library_id: Optional library ID to filter tags. If None, returns all tags.
            id_filter: Filter by tag ID (from create_filter_dependencies).
            sort_field: Field to sort by (default: 'name').
            search: Search term for tag name field.
            search_ignore_case: Case-insensitive search (default: True).
            limit: Number of results per page (pagination).
            offset: Number of results to skip (pagination).

        Injected Dependencies:
            tag_service: The tag service for database operations.
            filters: Pre-constructed filters from dependencies.

        Returns:
            Paginated list of tags matching the criteria.
        """
        filters.append(TagLibraryFilter(libraries))

        tags, total = await tag_service.list_and_count(*filters, uniquify=True)
        return tag_service.to_schema(tags, total, filters, schema_type=TagRead)
