# #region LLMAnalysisScheduler [C:3] [TYPE Module] [SEMANTICS scheduler, llm, validation, task, cron]
# @BRIEF Provides helper functions to schedule LLM-based validation tasks.
# @LAYER Domain
# @RELATION DEPENDS_ON -> [SchedulerService]

from typing import Any

from ...core.logger import belief_scope, logger
from ...dependencies import get_scheduler_service, get_task_manager


# #region schedule_dashboard_validation [TYPE Function]
# @BRIEF Schedules a recurring dashboard validation task.
# @SIDE_EFFECT  Adds a job to the scheduler service.
def schedule_dashboard_validation(dashboard_id: str, cron_expression: str, params: dict[str, Any]):
    with belief_scope("schedule_dashboard_validation", f"dashboard_id={dashboard_id}"):
        scheduler = get_scheduler_service()
        task_manager = get_task_manager()

        job_id = f"llm_val_{dashboard_id}"

        async def job_func():
            await task_manager.create_task(
                plugin_id="llm_dashboard_validation",
                params={
                    "dashboard_id": dashboard_id,
                    **params
                }
            )

        scheduler.add_job(
            job_func,
            "cron",
            id=job_id,
            replace_existing=True,
            **_parse_cron(cron_expression)
        )
        logger.info(f"Scheduled validation for dashboard {dashboard_id} with cron {cron_expression}")
# #endregion schedule_dashboard_validation

# #region _parse_cron [TYPE Function]
# @BRIEF Basic cron parser placeholder.
def _parse_cron(cron: str) -> dict[str, str]:
    # Basic cron parser placeholder
    parts = cron.split()
    if len(parts) != 5:
        return {}
    return {
        "minute": parts[0],
        "hour": parts[1],
        "day": parts[2],
        "month": parts[3],
        "day_of_week": parts[4]
    }
# #endregion _parse_cron

# #endregion LLMAnalysisScheduler
