import time

from textual.app import App, ComposeResult
from textual.widgets import Button, Static
from textual import work


class WorkerApp(App):
    def compose(self) -> ComposeResult:
        yield Static("Status: idle", id="status")
        yield Button("Start Work", id="start")
        yield Button("Fail Work", id="fail")

    @work(thread=True)
    def do_work(self) -> str:
        time.sleep(0.1)
        return "done"

    @work(thread=True)
    def do_failing_work(self) -> str:
        raise ValueError("Worker failed")

    def on_button_pressed(self, event: Button.Pressed) -> None:
        if event.button.id == "start":
            self.do_work()
        elif event.button.id == "fail":
            self.do_failing_work()
