import json

from spade.agent import Agent
from spade.behaviour import OneShotBehaviour
from spade.message import Message

from control_backend.core.config import settings


class BeliefTextAgent(Agent):
    class SendOnceBehaviourBlfText(OneShotBehaviour):
        async def run(self):
            to_jid = (
                settings.agent_settings.belief_collector_agent_name
                + "@"
                + settings.agent_settings.host
            )

            # Send multiple beliefs in one JSON payload
            payload = {
                "type": "belief_extraction_text",
                "beliefs": {
                    "user_said": [
                        "hello test",
                        "Can you help me?",
                        "stop talking to me",
                        "No",
                        "Pepper do a dance",
                    ]
                },
            }

            msg = Message(to=to_jid)
            msg.body = json.dumps(payload)
            await self.send(msg)
            print(f"Beliefs sent to {to_jid}!")

            self.exit_code = "Job Finished!"
            await self.agent.stop()

    async def setup(self):
        print("BeliefTextAgent started")
        self.b = self.SendOnceBehaviourBlfText()
        self.add_behaviour(self.b)
