from __future__ import annotations

from typing import Literal

from pydantic import BaseModel, Field, confloat, conlist
from pydantic.json_schema import SkipJsonSchema

from noqa_runner.domain.models.actions.base import BaseAction


class TapLocation(BaseModel):
    """Vision-detected tap location with bounding box, confidence and resolution."""

    bbox_2d: conlist(confloat(ge=0, le=1000), min_length=4, max_length=4) = Field(
        description="Bounding box in normalized coordinates [xmin, ymin, xmax, ymax] in range 0-1000. "
        "xmin, xmax: horizontal axis (0=left, 1000=right). "
        "ymin, ymax: vertical axis (0=top, 1000=bottom)."
    )
    confidence: float = Field(
        description="Confidence score from 0.0 to 1.0 indicating how certain the model is about the element location",
        ge=0.0,
        le=1.0,
    )
    resolution: SkipJsonSchema[dict[str, int] | None] = Field(
        default=None,
        description="Device screen resolution in pixels {width: int, height: int}",
    )


class VisionTap(BaseAction):
    """Tap on a mobile element using vision-based bounding box"""

    name: Literal["tap"] = "tap"
    element_description: str = Field(
        description="Brief description of the element location"
    )
    location: SkipJsonSchema[TapLocation | None] = Field(
        default=None,
        description="Vision-detected tap location with bounding box, confidence and resolution",
    )

    def get_action_description(self) -> str:
        """Get description of tap action"""
        return f"Tapped on: {self.element_description}"
