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

class JobPosting(BaseModel):
    skill: str
    description: str
    duties: str

class JobBoard(App):
    clock: Clock
    store: ObjectStore
    job_board: list[JobPosting] = []
    
    def __init__(self, clock: Clock, store: ObjectStore):
        super().__init__(clock, store)
        self.job_board = []
        
        
    def add_posting(self, posting: JobPosting) -> str:
        self.job_board.append(posting)
        self.on_change()
        return "Posting added"
    
    
    def get_postings(self) -> list:
        return [j.model_dump() for j in self.job_board]
    
    def get_posting_markdown(self, posting: JobPosting) -> str:
        return f"Skill: {posting.skill}\nDescription: {posting.description}\nDuties: {posting.duties}"
    
    def get_postings_as_text(self) -> str:
        postings = self.get_postings()
        table = "| Skill | Description | Duties \n"
        table += "| --- | --- | --- | \n"
        for posting in postings:
            table += f"| {posting['skill']} | {posting['description']} | {posting['duties']} \n"
        return table
    
    def clear_postings(self) -> str:
        self.job_board = []
        self.on_change()
        return "Postings cleared"
    
    def get_functions(self):
        def add_job_posting(
            skill: Annotated[str, "The skill required for the job posting"],
            description: Annotated[str, "A description of the job posting"],
            duties: Annotated[str, "The duties of the job posting"]
            ) -> str:
            return self.add_posting(JobPosting(skill=skill, description=description, duties=duties))
        
        def get_job_postings() -> list:
            return self.get_postings()
        
        def clear_job_postings() -> str:
            return self.clear_postings()
        
        return[
            (add_job_posting, "Adds a new skill posting to the job board"),
            (get_job_postings, "Returns the current job postings"),
            (clear_job_postings, "Clears all job postings")
        ]
    
    def on_change(self):
        self.store.save("job_board", {
            "postings": self.get_postings()
        }, namespace="job_board")