import inject
import markdown
from litestar import (
    Controller,
    get,
)
from litestar.exceptions import HTTPException
from litestar.response import Template

from server.modules.descriptions import CharactersService
from server.modules.recognizer import RecognizerService


class DescriptionController(Controller):
    path = "/"

    @get("/")
    async def dogs(self) -> Template:
        return Template(template_name="dogs.html")

    @get("/cats")
    async def cats(self) -> Template:
        return Template(template_name="cats.html")

    @get("/contacts")
    async def contacts(self) -> Template:
        return Template(template_name="contacts.html")

    @get("/donate")
    async def donate(self) -> Template:
        return Template(template_name="donate.html")

    @get("/dogs-characteristics")
    async def dogs_characteristics(self) -> Template:
        characters_service: CharactersService = inject.instance(CharactersService)
        breeds = await characters_service.get_characters("dog")
        return Template(template_name="dogs-characteristics.html", context={"breeds": breeds})

    @get("/dogs-characteristics/{name:str}")
    async def beer_description(self, name: str) -> Template:
        characters_service: CharactersService = inject.instance(CharactersService)
        breed = await characters_service.get_character(name)
        if breed is None:
            raise HTTPException(status_code=404, detail="Порода не найдена")
        recognizer_service: RecognizerService = inject.instance(RecognizerService)
        images = await recognizer_service.images_dogs()
        return Template(
            template_name="beers-description.html",
            context={
                "text": markdown.markdown(breed.description or ""),
                "title": breed.name,
                "images": [f"/static/assets/dog/{name}/{i}" for i in images[name]],
            },
        )
