from datetime import UTC, datetime
from uuid import uuid4

import inject
from litestar import (
    Controller,
    post,
)
from pydantic import BaseModel

from server.modules.descriptions import CharactersService
from server.modules.rate import Vote, VotesService


class VoteReq(BaseModel):
    attachment_id: str
    beerd_name: str
    rate: int

    def to_domain(self, name_id_convert: dict) -> Vote:
        return Vote(
            id=str(uuid4()),
            attachment_id=self.attachment_id,
            beerd_id=name_id_convert[self.beerd_name],
            rate=self.rate,
            created_at=datetime.now(UTC),
        )


class VoteController(Controller):
    path = "/votes"

    @post("/do")
    async def beerds_dogs(self, data: VoteReq) -> dict:
        rate_service: VotesService = inject.instance(VotesService)
        characters_service: CharactersService = inject.instance(CharactersService)
        breeds = await characters_service.get_characters("dog")
        await rate_service.add_vote(data.to_domain({b.name: b.id for b in breeds}))
        return {"success": True}
