from __future__ import annotations

from pathlib import Path
from typing import Any

from pydantic import BaseModel, Field, field_validator


class TaskConstraints(BaseModel):
    max_attempts: int = Field(default=3, ge=1, le=10)
    test_command: str | None = None
    lint_command: str | None = None
    type_check_command: str | None = None
    security_command: str | None = None
    build_command: str | None = None
    task_mode: str | None = Field(
        default=None,
        pattern="^(focused_file_repair|failing_test_repair|feature_build|refactor|analysis_only|general_coding)$",
    )
    engine: str | None = Field(default=None, pattern="^(langgraph|microsoft)$")
    sandbox_image: str | None = None
    use_docker: bool | None = None
    command_timeout_seconds: int | None = Field(default=None, ge=1, le=3600)
    model_overrides: dict[str, str] = Field(default_factory=dict)
    extra: dict[str, Any] = Field(default_factory=dict)


class TaskRequest(BaseModel):
    goal: str = Field(min_length=1)
    repo_path: Path
    constraints: TaskConstraints = Field(default_factory=TaskConstraints)

    @field_validator("repo_path")
    @classmethod
    def normalize_repo_path(cls, value: Path) -> Path:
        return value.expanduser()
