from datetime import datetime
from typing import Any

from pydantic import BaseModel, Field, field_validator


class BackupReportRequest(BaseModel):
    name: str = Field(..., min_length=1)
    source: str | None = None
    target: str | None = None
    schedule_interval_seconds: int | None = Field(None, gt=0)
    started_at: datetime
    ended_at: datetime | None = None
    status: str = Field(..., pattern="^(success|failure|in_progress)$")
    bytes_transferred: int | None = Field(None, ge=0)
    duration_ms: int | None = None
    error_message: str | None = None
    details: dict[str, Any] | None = None

    @field_validator("ended_at", "duration_ms")
    @classmethod
    def validate_success_fields(cls, v, info):
        if info.data.get("status") == "success" and v is None:
            raise ValueError(f"{info.field_name} is required when status is 'success'")
        return v


class BackupJobResponse(BaseModel):
    id: str
    name: str
    source: str | None
    target: str | None
    schedule_interval_seconds: int | None
    created_at: int


class BackupRunResponse(BaseModel):
    id: str
    job_id: str
    started_at: int
    ended_at: int | None
    status: str
    bytes_transferred: int | None
    duration_ms: int | None
    error_message: str | None
    details_json: dict[str, Any] | None
    created_at: int


class BackupAlertResponse(BaseModel):
    id: str
    job_id: str
    run_id: str | None
    alert_type: str
    severity: str
    message: str
    acknowledged: bool
    resolved_at: int | None
    created_at: int


class BackupDashboardSummary(BaseModel):
    total_jobs: int
    success_rate_24h: float
    active_alerts: int
    last_failed_at: int | None
