# generated by datamodel-codegen:
#   filename:  websocket-communication.json
#   timestamp: 2026-02-04T15:38:26+00:00

from __future__ import annotations

from enum import StrEnum
from typing import Any, Literal

from pydantic import BaseModel, ConfigDict

from engine.server.ws.handlers import flow


class WebSocketMessage(BaseModel):
    """
    Base WebSocket message structure
    """

    model_config = ConfigDict(
        populate_by_name=True,
    )
    command: str
    """
    Command type
    """
    flow_id: str | None = None
    """
    Flow ID for the command
    """


class SessionInfo(BaseModel):
    """
    Information about an active session
    """

    model_config = ConfigDict(
        populate_by_name=True,
    )
    flowId: str
    """
    Flow ID
    """
    status: str
    """
    Session status
    """
    browser: str
    """
    Browser type
    """
    flowName: str | None = None
    """
    Flow name
    """
    startedAt: int
    """
    Start timestamp
    """


class Flow(BaseModel):
    """
    Flow to execute
    """

    model_config = ConfigDict(
        populate_by_name=True,
    )
    id: str
    name: str
    actions: list[dict[str, Any]]
    edges: list[dict[str, Any]]


class Mode(StrEnum):
    full = 'full'
    partial = 'partial'


class RunCommand(WebSocketMessage):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    command: Literal['run']
    """
    Command type
    """
    flow: Flow
    """
    Flow to execute
    """
    mode: Mode | None = 'full'
    recording: dict[str, Any] | None = None


class RecordCommand(WebSocketMessage):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    command: Literal['record']
    """
    Command type
    """
    flow_id: str | None = None
    """
    Flow ID for the command
    """
    url: str | None = None
    config: dict[str, Any] | None = {}


class PauseRecordingCommand(WebSocketMessage):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    command: Literal['pause_recording']
    """
    Command type
    """
    session_id: str
    """
    Recording session ID
    """


class ResumeRecordingCommand(WebSocketMessage):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    command: Literal['resume_recording']
    """
    Command type
    """
    session_id: str
    """
    Recording session ID
    """


class StopRecordingCommand(WebSocketMessage):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    command: Literal['stop_recording']
    """
    Command type
    """
    session_id: str
    """
    Recording session ID
    """


class FinishRecordingCommand(WebSocketMessage):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    command: Literal['finish_recording']
    """
    Command type
    """
    session_id: str
    """
    Recording session ID
    """
    flow_name: str | None = 'Recorded Flow'


class GetRecordingStatusCommand(WebSocketMessage):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    command: Literal['get_recording_status']
    """
    Command type
    """
    session_id: str | None = None


class ListRecordingsCommand(WebSocketMessage):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    command: Literal['list_recordings']
    """
    Command type
    """


class ConvertRecordingToFlowCommand(WebSocketMessage):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    command: Literal['convert_recording_to_flow']
    """
    Command type
    """
    session_id: str
    """
    Recording session ID
    """
    flow_name: str | None = 'Recorded Flow'


class GetRealtimeActionsCommand(WebSocketMessage):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    command: Literal['get_realtime_actions']
    """
    Command type
    """
    session_id: str
    """
    Recording session ID
    """


class GetLatestRealtimeActionCommand(WebSocketMessage):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    command: Literal['get_latest_realtime_action']
    """
    Command type
    """
    session_id: str
    """
    Recording session ID
    """


class StopCommand(WebSocketMessage):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    command: Literal['stop']
    """
    Command type
    """
    flow_id: str | None = None
    """
    Flow ID for the command
    """


class CloseCommand(WebSocketMessage):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    command: Literal['close']
    """
    Command type
    """


class ListSessionsCommand(WebSocketMessage):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    command: Literal['list_sessions']
    """
    Command type
    """


class CloseSessionCommand(WebSocketMessage):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    command: Literal['close_session']
    """
    Command type
    """
    flow_id: str | None = None
    """
    Flow ID for the command
    """


class RestartEngineCommand(WebSocketMessage):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    command: Literal['restart_engine']
    """
    Command type
    """


class CloseEngineCommand(WebSocketMessage):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    command: Literal['close_engine']
    """
    Command type
    """


class GetEngineStatusCommand(WebSocketMessage):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    command: Literal['get_engine_status']
    """
    Command type
    """


class StartPerformanceScanCommand(WebSocketMessage):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    command: Literal['start_performance_scan']
    """
    Command type
    """
    test_id: str
    """
    Performance test ID
    """
    url: str
    """
    URL to navigate to and record
    """


class StopPerformanceScanCommand(WebSocketMessage):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    command: Literal['stop_performance_scan']
    """
    Command type
    """
    test_id: str
    """
    Performance test ID
    """
    session_id: str
    """
    Recording session ID
    """


class RunLoadTestCommand(WebSocketMessage):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    command: Literal['run_load_test']
    """
    Command type
    """
    test_id: str
    """
    Performance test ID
    """
    config: dict[str, Any]
    """
    Load test configuration
    """
    auth_config: dict[str, Any] | None = None
    """
    Authentication configuration for load test
    """


class StopLoadTestCommand(WebSocketMessage):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    command: Literal['stop_load_test']
    """
    Command type
    """
    test_id: str
    """
    Performance test ID
    """


class WebSocketResponse(BaseModel):
    """
    Base WebSocket response structure
    """

    model_config = ConfigDict(
        populate_by_name=True,
    )
    command: str
    """
    Command that was executed
    """
    success: bool
    """
    Whether the command succeeded
    """
    message: str | None = None
    """
    Response message
    """


class Report(BaseModel):
    """
    Flow execution report
    """

    model_config = ConfigDict(
        populate_by_name=True,
    )
    actions: list[dict[str, Any]] | None = None
    variables: dict[str, Any] | None = None
    parameters: dict[str, Any] | None = None
    output: dict[str, Any] | None = None


class RunResponse(WebSocketResponse):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    command: Literal['run']
    """
    Command that was executed
    """
    mode: Mode
    reports: list[Report]


class ListSessionsResponse(WebSocketResponse):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    command: Literal['list_sessions']
    """
    Command that was executed
    """
    sessions: list[SessionInfo]


class EngineStatusResponse(WebSocketResponse):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    command: Literal['get_engine_status']
    """
    Command that was executed
    """
    status: str
    """
    Engine status
    """
    uptime: int
    """
    Engine uptime in seconds
    """
    active_sessions: int
    """
    Number of active sessions
    """


class Command(StrEnum):
    """
    Command that was executed
    """

    record = 'record'
    pause_recording = 'pause_recording'
    resume_recording = 'resume_recording'
    stop_recording = 'stop_recording'
    finish_recording = 'finish_recording'


class RecordingResponse(WebSocketResponse):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    command: Command
    """
    Command that was executed
    """
    session_id: str | None = None
    data: dict[str, Any] | None = None


class RecordingStatusResponse(WebSocketResponse):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    command: Literal['get_recording_status']
    """
    Command that was executed
    """
    session_info: dict[str, Any] | None = None


class ListRecordingsResponse(WebSocketResponse):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    command: Literal['list_recordings']
    """
    Command that was executed
    """
    recordings: list[dict[str, Any]] | None = []


class DebugRunCommand(WebSocketMessage):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    command: Literal['debug_run']
    """
    Command type
    """
    session_id: str
    """
    Unique debug session identifier
    """
    flow: dict[str, Any]
    """
    Flow to execute in debug mode
    """
    breakpoints: list[str] | None = []
    """
    List of action IDs where execution should pause
    """
    mode: Literal['debug'] = 'debug'


class DebugStepCommand(WebSocketMessage):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    command: Literal['debug_step']
    """
    Command type
    """
    session_id: str
    """
    Debug session identifier
    """


class DebugContinueCommand(WebSocketMessage):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    command: Literal['debug_continue']
    """
    Command type
    """
    session_id: str
    """
    Debug session identifier
    """


class DebugPauseCommand(WebSocketMessage):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    command: Literal['debug_pause']
    """
    Command type
    """
    session_id: str
    """
    Debug session identifier
    """


class DebugStopCommand(WebSocketMessage):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    command: Literal['debug_stop']
    """
    Command type
    """
    session_id: str
    """
    Debug session identifier
    """


class Command1(StrEnum):
    """
    Command that was executed
    """

    debug_run = 'debug_run'
    debug_step = 'debug_step'
    debug_continue = 'debug_continue'
    debug_pause = 'debug_pause'
    debug_stop = 'debug_stop'
    debug_error = 'debug_error'


class State(StrEnum):
    """
    Current debug session state
    """

    ready = 'ready'
    running = 'running'
    paused = 'paused'
    stepping = 'stepping'
    completed = 'completed'
    error = 'error'
    stopped = 'stopped'


class DebugResponse(WebSocketResponse):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    command: Command1
    """
    Command that was executed
    """
    session_id: str | None = None
    """
    Debug session identifier
    """
    state: State | None = None
    """
    Current debug session state
    """
    data: dict[str, Any] | None = None
    """
    Additional debug data (action results, variables, etc.)
    """
    error: str | None = None
    """
    Error message if command failed
    """


class WebsocketCommunication(BaseModel):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    commands: (
        list[
            RunCommand
            | RecordCommand
            | PauseRecordingCommand
            | ResumeRecordingCommand
            | StopRecordingCommand
            | FinishRecordingCommand
            | GetRecordingStatusCommand
            | ListRecordingsCommand
            | ConvertRecordingToFlowCommand
            | GetRealtimeActionsCommand
            | GetLatestRealtimeActionCommand
            | StopCommand
            | CloseCommand
            | ListSessionsCommand
            | CloseSessionCommand
            | RestartEngineCommand
            | CloseEngineCommand
            | GetEngineStatusCommand
            | StartPerformanceScanCommand
            | StopPerformanceScanCommand
            | RunLoadTestCommand
            | StopLoadTestCommand
            | DebugRunCommand
            | DebugStepCommand
            | DebugContinueCommand
            | DebugPauseCommand
            | DebugStopCommand
        ]
        | None
    ) = None
    responses: (
        list[
            RunResponse
            | ListSessionsResponse
            | EngineStatusResponse
            | RecordingResponse
            | RecordingStatusResponse
            | ListRecordingsResponse
            | DebugResponse
            | WebSocketResponse
        ]
        | None
    ) = None


class WebSocketCommunicationModels(BaseModel):
    """
    Schema for WebSocket API commands and responses
    """

    model_config = ConfigDict(
        populate_by_name=True,
    )
    websocketCommunication: WebsocketCommunication | None = None
