from typing import TYPE_CHECKING, Optional

from litestar import Controller, get
from litestar.contrib.repository.filters import LimitOffset, SearchFilter
from litestar.di import Provide
from sqlalchemy.ext.asyncio import AsyncSession

from app.domain.company import Company
from app.domain.mass_machine_00558 import (
    Machine_00558,
    Machine_00558_ReadDTO,
    Machine_00558_WriteDTO,
    Repository,
    Service,
)
from app.lib.responses import ObjectListResponse, ObjectResponse

if TYPE_CHECKING:
    from sqlalchemy.ext.asyncio import AsyncSession


DETAIL_ROUTE = "/{machine_id:int}"


async def provides_service(db_session: AsyncSession, company_id: int) -> Service:
    """Constructs repository and service objects for the request."""
    from app.controllers.company import provides_service

    company_service = provides_service(db_session)
    company = await company_service.get(company_id)
    return Service(Repository(session=db_session, company=company))


async def get_company(db_session: AsyncSession, company_id: int) -> Company:
    from app.controllers.company import provides_service

    company_service = provides_service(db_session)
    return await company_service.get(company_id)


class MachineController_00558(Controller):
    dto = Machine_00558_WriteDTO
    return_dto = Machine_00558_ReadDTO
    path = "/company/{company_id:int}/machines-00558"
    dependencies = {
        "service": Provide(provides_service),
    }
    tags = ["Machines-00558"]

    @get()
    async def get_machines(
        self,
        service: Service,
        search: Optional[str] = None,
    ) -> ObjectListResponse[Machine_00558]:
        filters = [
            LimitOffset(limit=20, offset=0),
        ]

        if search is not None:
            filters.append(
                SearchFilter(
                    field_name="caption",
                    value=search,
                ),
            )

        content = await service.list(*filters)
        return ObjectListResponse(content=content)

    @get(DETAIL_ROUTE)
    async def get_machine(
        self, service: Service, machine_id: int
    ) -> ObjectResponse[Machine_00558]:
        content = await service.get(machine_id)
        return ObjectResponse(content=content)
