from __future__ import annotations

from typing import Any, Dict, Optional

from app.config import settings


def _psycopg_url() -> str:
    url = settings.database_url
    if url.startswith("postgresql+psycopg://"):
        return "postgresql://" + url.split("postgresql+psycopg://", 1)[1]
    return url


def prepare_task(
    *,
    task_id: Optional[str] = None,
    conversation_id: Optional[str] = None,
    prep_type: str = "generic",
) -> Dict[str, Any]:
    """Executa a preparação operacional sem subprocess nem paths absolutos."""
    from scripts.prepare_task import run_preparation

    return run_preparation(
        task_id=task_id,
        conversation_id=conversation_id,
        prep_type=prep_type,
        database_url=_psycopg_url(),
    )
