// Copyright 2026. Licensed under Apache-2.0. See LICENSE.
// Generated by CLI Printing Press (https://github.com/mvanhorn/cli-printing-press). DO NOT EDIT.

package device

import (
	"context"
	"time"

	"ble-session-appliance-pp-cli/internal/cliutil"
)

type StatusSnapshot struct {
	Device      string         `json:"device"`
	Transport   string         `json:"transport"`
	SessionMode string         `json:"session_mode"`
	ObservedAt  string         `json:"observed_at"`
	Telemetry   map[string]any `json:"telemetry"`
}

type CommandResult struct {
	Command            string `json:"command"`
	Transport          string `json:"transport"`
	CharacteristicUUID string `json:"characteristic_uuid"`
	PayloadHex         string `json:"payload_hex"`
	Safety             string `json:"safety"`
	ValidationStatus   string `json:"validation_status,omitempty"`
	DryRun             bool   `json:"dry_run"`
	VerifyNoop         bool   `json:"verify_noop,omitempty"`
	Reason             string `json:"reason,omitempty"`
}

type Transport interface {
	Status(context.Context) (StatusSnapshot, error)
	// ExecuteCommand runs a command. args are the command's positional CLI
	// arguments for parameterized commands; the replay transport ignores them.
	ExecuteCommand(ctx context.Context, command CommandDefinition, args []string, dryRun bool) (CommandResult, error)
}

type ReplayTransport struct{}

func NewReplayTransport() *ReplayTransport {
	return &ReplayTransport{}
}

func (t *ReplayTransport) Status(ctx context.Context) (StatusSnapshot, error) {
	telemetry := map[string]any{}
	for _, field := range StatusFields {
		telemetry[field.Name] = map[string]any{
			"source_characteristic_uuid": field.SourceCharacteristicUUID,
			"sample_cadence":             field.SampleCadence,
			"value":                      nil,
		}
	}
	return StatusSnapshot{
		Device:      DisplayName,
		Transport:   "replay",
		SessionMode: SessionMode,
		ObservedAt:  time.Now().UTC().Format(time.RFC3339),
		Telemetry:   telemetry,
	}, nil
}

func (t *ReplayTransport) ExecuteCommand(ctx context.Context, command CommandDefinition, args []string, dryRun bool) (CommandResult, error) {
	_ = args // replay describes the captured payload; parameterized encoding is a live-codec concern
	verifyNoop := cliutil.IsVerifyEnv()
	return CommandResult{
		Command:            command.Name,
		Transport:          commandTransportName(verifyNoop),
		CharacteristicUUID: command.CharacteristicUUID,
		PayloadHex:         command.PayloadHex,
		Safety:             command.Safety,
		ValidationStatus:   command.ValidationStatus,
		DryRun:             dryRun || verifyNoop,
		VerifyNoop:         verifyNoop,
		Reason:             commandNoopReason(verifyNoop),
	}, nil
}

func commandTransportName(verifyNoop bool) string {
	if verifyNoop {
		return "verify-replay"
	}
	return "replay"
}

func commandNoopReason(verifyNoop bool) string {
	if verifyNoop {
		return "verify_short_circuit"
	}
	return ""
}
