import uuid
from uuid import UUID

from pydantic import BaseModel

from huesoporro.models import Chatbot
from huesoporro.svc.chatbot_svcs import (
    CreateChatbotSvc,
    GetChatbotByUserIdSvc,
    UpdateChatbotSvc,
)


class CreateOrUpdateChatbotAction(BaseModel):
    create_chatbot_svc: CreateChatbotSvc
    update_chatbot_svc: UpdateChatbotSvc
    get_chatbot_by_user_id_svc: GetChatbotByUserIdSvc

    async def run(
        self,
        user_id: UUID,
        automatic_generation_timer: int = 300,
        automatic_quote_timer: int = 500,
        mods: list[str] | None = None,
    ) -> Chatbot:
        mods = mods or []
        chatbot = await self.get_chatbot_by_user_id_svc.run(user_id=user_id)
        if chatbot:
            chatbot.automatic_generation_timer = automatic_generation_timer
            chatbot.automatic_quote_timer = automatic_quote_timer
            chatbot.mods = mods
            return await self.update_chatbot_svc.run(chatbot=chatbot)
        chatbot = Chatbot(
            id=uuid.uuid4(),
            user_id=user_id,
            automatic_generation_timer=automatic_generation_timer,
            automatic_quote_timer=automatic_quote_timer,
            mods=mods,
        )
        return await self.create_chatbot_svc.run(chatbot=chatbot)
