"""Advanced scanning session models and helpers."""

from __future__ import annotations

from datetime import UTC, datetime
from typing import Any

from pydantic import BaseModel, Field

from sentra.models import DeviceEvidence


def session_now() -> datetime:
    return datetime.now(UTC)


class CachedServiceMetadata(BaseModel):
    host: str
    port: int
    protocol: str
    service: str
    banner: str | None = None
    confidence: float = Field(default=0.5, ge=0.0, le=1.0)
    fingerprint_source: str = "port-map"
    protocols: list[str] = Field(default_factory=list)
    product: str | None = None
    normalized_product: str | None = None
    version: str | None = None
    metadata: dict[str, Any] = Field(default_factory=dict)
    evidence: list[dict[str, Any]] = Field(default_factory=list)
    updated_at: datetime = Field(default_factory=session_now)


class ScanCache(BaseModel):
    entries: dict[str, CachedServiceMetadata] = Field(default_factory=dict)


class ScanResumeState(BaseModel):
    target: str
    hosts_total: int
    completed_hosts: list[str] = Field(default_factory=list)
    devices: list[DeviceEvidence] = Field(default_factory=list)
    created_at: datetime = Field(default_factory=session_now)
    updated_at: datetime = Field(default_factory=session_now)

