# src/chitai/controllers/publisher.py

# Standard library
from typing import Annotated

# Third-party libraries
from litestar import Controller, get
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 PublisherService
from chitai.schemas import PublisherRead
from chitai.services.filters.publisher import PublisherLibraryFilter


class PublisherController(Controller):
    """Controller for managing publisher information."""

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

    @get()
    async def list_publishers(
        self,
        publisher_service: PublisherService,
        libraries: list[int] | None = None,
        filters: Annotated[list[FilterTypeT], Dependency(skip_validation=True)] = [],
    ) -> OffsetPagination[PublisherRead]:
        """
        List publishers 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 publishers. If None, returns all publishers.
            id_filter: Filter by publisher ID (from create_filter_dependencies).
            sort_field: Field to sort by (default: 'name').
            search: Search term for publisher 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:
            publisher_service: The publisher service for database operations.
            filters: Pre-constructed filters from dependencies.

        Returns:
            Paginated list of publishers matching the criteria.
        """

        filters.append(PublisherLibraryFilter(libraries))

        publishers, total = await publisher_service.list_and_count(
            *filters, uniquify=True
        )
        return publisher_service.to_schema(
            publishers, total, filters, schema_type=PublisherRead
        )
