"""Inline permission question domain model.

Represents a single-file permission request that can be presented
inline in the conversation stream via the PermissionQuestionWidget.

Based on issue #997 — feat(tui): implement Permission Question Widget.
"""

from __future__ import annotations

from enum import StrEnum

from pydantic import BaseModel, ConfigDict, Field

__all__ = [
    "InlinePermissionQuestion",
    "PermissionDecision",
    "PermissionRequestType",
]


class PermissionRequestType(StrEnum):
    """Type of permission being requested."""

    FILE_WRITE = "file_write"
    FILE_DELETE = "file_delete"
    FILE_READ = "file_read"
    SHELL_EXEC = "shell_exec"
    NETWORK = "network"


class PermissionDecision(StrEnum):
    """Decision made on a permission request."""

    ALLOW_ONCE = "allow_once"
    ALLOW_ALWAYS = "allow_always"
    REJECT_ONCE = "reject_once"
    REJECT_ALWAYS = "reject_always"


class InlinePermissionQuestion(BaseModel):
    """Domain model for an inline permission question.

    Represents a single-file permission request that can be displayed
    inline in the conversation stream.  For multi-file operations the
    full PermissionsScreen is used instead.
    """

    file_path: str = Field(
        ...,
        min_length=1,
        description="Path of the file the actor wants to operate on.",
    )
    request_type: PermissionRequestType = Field(
        ...,
        description="Type of operation being requested.",
    )
    diff_content: str = Field(
        default="",
        description="Unified diff content showing proposed changes (may be empty).",
    )
    actor_name: str = Field(
        default="actor",
        min_length=1,
        description="Name of the actor requesting permission.",
    )

    model_config = ConfigDict(
        str_strip_whitespace=True,
        validate_assignment=True,
    )

    @property
    def has_diff(self) -> bool:
        """Return True when diff content is available."""
        return bool(self.diff_content.strip())

    def description_line(self) -> str:
        """Return a human-readable one-line description of the request."""
        verb_map: dict[PermissionRequestType, str] = {
            PermissionRequestType.FILE_WRITE: "write to",
            PermissionRequestType.FILE_DELETE: "delete",
            PermissionRequestType.FILE_READ: "read",
            PermissionRequestType.SHELL_EXEC: "execute",
            PermissionRequestType.NETWORK: "access network via",
        }
        verb = verb_map.get(self.request_type, "operate on")
        return f"The actor wants to {verb}:"
