import json
from .app import App
from .clock import Clock
from .object_store import ObjectStore
from autogen import AssistantAgent
from pydantic import BaseModel
from enum import Enum
from typing import Annotated


class Task(BaseModel):
    assignee: Annotated[str, "The person assigned to the task"]
    name: Annotated[str, "The name of the task"]
    description: Annotated[str, "The description of the task"]
    status: Annotated[str, "The status of the task it only accepts 'pending' or 'done'"]
    type: Annotated[str, "The type of the task. Discussion is a task that will benefit from a meeting. Work is a task that can be done by the assignee and reported to the team"]
    participants: Annotated[list[str], "The additional participants of the task. For discussion tasks, these will participate in the discussion"] = None
    schedule_for: Annotated[str, "The date the task is scheduled for. If set to None it will be executed ASAP"] = None

class TaskManager(App):
    tasks: list[Task] = []
    
    def __init__(self, clock: Clock, store: ObjectStore):
        super().__init__(clock, store)
        self.tasks = []
    
    def create_task(self, task: Annotated[Task, "The task to be created"]) -> str:
        task.status = "pending"
        self.tasks.append(task)
        self.on_change(task)
        return f"Task {task.name} created"
    
    def get_tasks(self) -> list:
        return [t for t in self.tasks if t.status not in ["done", "completed"] ]
    
    def get_task(self, name: str) -> str:
        for task in self.tasks:
            if task.name == name:
                return self.task_to_mkd(task)
        return None    
    
    def task_to_mkd(self, task: Task) -> str:
        text = f"Task: {task.name}\n"
        text += f"Description: {task.description}\n"
        text += f"Assignee: {task.assignee}\n"
        text += f"Status: {task.status}\n"
        text += f"Scheduled For: {task.schedule_for}\n"
        return text
        
    
    def complete_task(self, name: str) -> str:
        for task in self.tasks:
            if task.name == name:
                task.status = "done"
                self.on_change(task)
                return "Task completed"
        return "Task not found"
    
    def delete_task(self, name: str) -> str:
        for task in self.tasks:
            if task.name == name:
                self.tasks.remove(task)
                self.on_change(task)
                return "Task deleted"
        return "Task not found"
    
    def get_tasks_as_text(self) -> str:
        """Returns the tasks as a markdown table"""
        tasks = self.get_tasks()
        table = "| Name | Description | Assignee | Status \n"
        table += "| --- | --- | --- | --- | \n"
        for task in tasks:
            table += f"| {task.name} | {task.description} | {task.assignee} | {task.status} \n"
        return table

    def to_json(self):
        return  [json.loads(t.model_dump_json()) for t in self.tasks]
    
    def on_change(self, task: Task = None):
        self.store.save("tasks", self.to_json(), namespace="task_manager")
    
    def get_functions(self):
        def create_task(task: Task) -> str:
            return self.create_task(task)
        
        def get_tasks() -> str:
            return self.get_tasks_as_text()
        
        def get_task(name: str) -> dict:
            return self.get_task(name)
        
        def complete_task(name: str) -> str:
            return self.complete_task(name)
        
        def delete_task(name: str) -> str:
            return self.delete_task(name)
        
        return [
            (create_task, "Use this tool to create a task"), 
            (get_tasks, "Use this tool to get the tasks"), 
            (get_task, "Use this tool to get a task"), 
            (complete_task, "Use this tool to complete a task"), 
            (delete_task, "Use this tool to delete a task")
        ]
    
  