"""Pydantic models for project endpoints."""

from __future__ import annotations

from datetime import datetime

from pydantic import BaseModel, Field

from peon_mcp.work_logs.schemas import WorkLogResponse


class K8sConfig(BaseModel):
    namespace: str = ""
    app_labels: list[str] = Field(default_factory=list)
    loki_url: str = ""
    log_query_template: str = ""
    triage_interval_minutes: int = 15
    error_patterns: list[str] = Field(
        default_factory=lambda: ["ERROR", "Traceback", "Exception", "CRITICAL"]
    )
    noise_patterns: list[str] = Field(
        default_factory=lambda: [
            "Connection reset by peer",
            "Broken pipe",
            "kube-probe",
        ]
    )


class DeployConfig(BaseModel):
    """Deploy pipeline configuration for a project.

    When enabled, a merged pull request to the project's repository will
    trigger a GitHub Actions workflow_dispatch event.
    """

    enabled: bool = False
    github_repo: str = ""
    """Target GitHub repository (e.g. 'owner/repo'). Defaults to the project's
    own github_url repo if empty."""
    workflow_id: str = ""
    """Workflow file name or numeric ID (e.g. 'docker-build.yml')."""
    ref: str = "main"
    """Branch, tag, or SHA to target for the workflow_dispatch."""
    inputs: dict[str, str] = Field(default_factory=dict)
    """Optional key/value inputs forwarded to the workflow_dispatch call."""


class ProjectResponse(BaseModel):
    id: str
    path: str
    github_url: str = ""
    capture_hostname: str = ""
    created_at: datetime
    owner_id: int | None = None
    owner_login: str | None = None


class CreateProjectRequest(BaseModel):
    id: str
    path: str = ""
    github_url: str = ""
    capture_hostname: str = ""


class UpdateProjectRequest(BaseModel):
    path: str | None = None
    github_url: str | None = None
    capture_hostname: str | None = None


class DurationStatsResponse(BaseModel):
    avg_duration: float | None = None
    min_duration: float | None = None
    max_duration: float | None = None
    completed_count: int


class PerformanceStatsResponse(BaseModel):
    total_lines_added: int
    total_lines_deleted: int
    total_tokens_used: int


class ProjectStatsResponse(BaseModel):
    feature_counts: dict[str, int]
    task_counts: dict[str, int]
    task_priority_counts: dict[str, int]
    total_logs: int
    recent_logs: list[WorkLogResponse]
    duration_stats: DurationStatsResponse
    performance_stats: PerformanceStatsResponse


class ActivityDataPoint(BaseModel):
    hour: str
    count: int


class ProjectActivityResponse(BaseModel):
    activity: list[ActivityDataPoint]


class ProjectRelationResponse(BaseModel):
    id: int
    project_id: str
    related_project_id: str
    created_at: datetime


class AddProjectRelationRequest(BaseModel):
    related_project_id: str
