import os

import requests

from dojo_sdk_core.dojos.model import Dojo
from dojo_sdk_core.settings import settings


# Add new dojos here
def load_dojos() -> list[Dojo]:
    return []


def collect_tasks(dojos: list[Dojo]) -> list[str]:
    return [dojo.get_id() + "/" + task.id for dojo in dojos for task in dojo.get_tasks_list()]


def run_tasks_by_name(task_names: list[str]) -> list[str]:
    """Validate task names exist in the tasks table via the orchestrator API."""
    api_key = os.getenv("DOJO_API_KEY")
    if not api_key:
        raise ValueError("DOJO_API_KEY environment variable is required")

    response = requests.post(
        f"{settings.dojo_http_endpoint}/tasks/by-name",
        json={"task_names": task_names},
        headers={"Authorization": f"Bearer {api_key}"},
    )
    response.raise_for_status()

    found_tasks = set(response.json().get("tasks", {}).keys())

    for task_name in task_names:
        if task_name not in found_tasks:
            raise ValueError(f"Task {task_name} not found in tasks table")

    return task_names
