from .app import App
from .clock import Clock
from .object_store import ObjectStore
from typing import Annotated

class Constitution(App):
    clock: Clock
    store: ObjectStore
    def __init__(self, clock: Clock, store: ObjectStore):
        self.clock = clock
        self.store = store
        self.mission = ""
        self.plan = ""
        self.initial_budget = 0
    
    def set_mission(self, mission: str) -> str:
        self.mission = mission
        self.on_change()
        return "Mission set"
    
    def set_strategic_plan(self, plan: Annotated[str, "A concise strategic plan for the team"]) -> str:
        self.plan = plan
        self.on_change()
        return "Plan set"
    
    def set_initial_budget(self, budget: float) -> str:
        self.initial_budget = budget
        self.on_change()
        return "Initial budget set"
    
    def get_initial_budget(self) -> float:
        return self.initial_budget
    
    def get_strategic_plan(self) -> str:   
        return self.plan
    
    def get_mission(self) -> str:
        return self.mission
    
    def on_change(self):
        self.store.save("constitution", {
            "mission": self.mission,
            "plan": self.plan,
            "initial_budget": self.initial_budget
        }, namespace="constitution")
        pass
    
    def get_constitution(self) -> str:
        if not self.mission:
            return ""
        constitution = "# Team's Constitution\n"
        constitution += f"## Mission\n{self.mission}\n"
        constitution += f"## Strategic Plan\n{self.plan}\n"
        constitution += f"## Initial Budget\n{self.initial_budget}\n"
        return constitution
        
        
    
    def get_functions(self):
        def set_mission(mission: str) -> str:
            return self.set_mission(mission)
        
        def get_mission() -> str:
            return self.get_mission()
        
        def set_strategic_plan(plan: str) -> str:
            return self.set_strategic_plan(plan)
        
        def set_initial_budget(budget: float) -> str:
            return self.set_initial_budget(budget)
        
        def get_initial_budget() -> float:
            return self.get_initial_budget()
        
        def get_strategic_plan() -> str:
            return self.get_strategic_plan()
        
        return [
            (set_mission, "Use this tool to set the mission of the team"),
            (get_mission, "Use this tool to get the mission of the team"),
            (set_strategic_plan, "Use this tool to set the strategic plan of the team"),
            (set_initial_budget, "Use this tool to set the initial budget of the team"),
            (get_initial_budget, "Use this tool to get the initial budget of the team"),
            (get_strategic_plan, "Use this tool to get the strategic plan of the team")
        ]