#include "RuntimeStateLayerModel.h"

const char* RuntimeStateLayerKindName(RuntimeStateLayerKind kind)
{
	switch (kind)
	{
	case RuntimeStateLayerKind::BasePersisted:
		return "base persisted";
	case RuntimeStateLayerKind::CommittedLive:
		return "committed live";
	case RuntimeStateLayerKind::TransientAutomation:
		return "transient automation";
	case RuntimeStateLayerKind::RenderLocal:
		return "render local";
	case RuntimeStateLayerKind::HealthConfig:
		return "health/config";
	default:
		return "unknown";
	}
}

int RuntimeStateLayerCompositionPrecedence(RuntimeStateLayerKind kind)
{
	switch (kind)
	{
	case RuntimeStateLayerKind::BasePersisted:
		return 0;
	case RuntimeStateLayerKind::CommittedLive:
		return 1;
	case RuntimeStateLayerKind::TransientAutomation:
		return 2;
	case RuntimeStateLayerKind::RenderLocal:
	case RuntimeStateLayerKind::HealthConfig:
	default:
		return -1;
	}
}

bool RuntimeStateLayerIsDurable(RuntimeStateLayerKind kind)
{
	return kind == RuntimeStateLayerKind::BasePersisted;
}

bool RuntimeStateLayerParticipatesInParameterComposition(RuntimeStateLayerKind kind)
{
	return RuntimeStateLayerCompositionPrecedence(kind) >= 0;
}

bool RuntimeStateLayerIsRenderLocal(RuntimeStateLayerKind kind)
{
	return kind == RuntimeStateLayerKind::RenderLocal;
}

RuntimeStateLayerKind ClassifyRuntimeStateField(RuntimeStateField field)
{
	switch (field)
	{
	case RuntimeStateField::PersistedLayerStack:
	case RuntimeStateField::PersistedParameterValues:
	case RuntimeStateField::StackPresets:
		return RuntimeStateLayerKind::BasePersisted;
	case RuntimeStateField::CommittedSessionParameterValues:
	case RuntimeStateField::CommittedLayerBypass:
	case RuntimeStateField::RuntimeCompileReloadFlags:
		return RuntimeStateLayerKind::CommittedLive;
	case RuntimeStateField::TransientOscOverlay:
	case RuntimeStateField::TransientAutomationCommitState:
		return RuntimeStateLayerKind::TransientAutomation;
	case RuntimeStateField::RenderLocalTemporalHistory:
	case RuntimeStateField::RenderLocalFeedbackState:
	case RuntimeStateField::RenderLocalInputFrames:
	case RuntimeStateField::RenderLocalOutputFrames:
		return RuntimeStateLayerKind::RenderLocal;
	case RuntimeStateField::RuntimeConfiguration:
	case RuntimeStateField::HealthTelemetry:
	default:
		return RuntimeStateLayerKind::HealthConfig;
	}
}

std::vector<RuntimeStateLayerDescriptor> GetRuntimeStateLayerInventory()
{
	return {
		{
			RuntimeStateLayerKind::BasePersisted,
			"Base persisted state",
			"RuntimeStore / LayerStackStore",
			"Survives restart",
			"Written to disk",
			"Default layer stack, shader selections, saved parameter values"
		},
		{
			RuntimeStateLayerKind::CommittedLive,
			"Committed live state",
			"RuntimeCoordinator / CommittedLiveState",
			"Current running session",
			"May request persistence depending on mutation policy",
			"Operator/session truth until changed again"
		},
		{
			RuntimeStateLayerKind::TransientAutomation,
			"Transient automation overlay",
			"RuntimeLiveState / RuntimeServiceLiveBridge",
			"High-rate and short-lived",
			"Not persisted directly",
			"Temporary OSC/automation target applied over committed truth"
		},
		{
			RuntimeStateLayerKind::RenderLocal,
			"Render-local state",
			"RenderEngine",
			"Render-thread/resource lifetime",
			"Not persisted",
			"Temporal history, feedback, input/output queues, and GL-local caches"
		},
		{
			RuntimeStateLayerKind::HealthConfig,
			"Health/config state",
			"RuntimeConfigStore / HealthTelemetry",
			"Config survives restart; health is observational",
			"Config is file-backed; health is reported, not composed",
			"Does not participate in parameter composition"
		}
	};
}

std::vector<RuntimeStateFieldDescriptor> GetRuntimeStateFieldInventory()
{
	return {
		{
			RuntimeStateField::PersistedLayerStack,
			ClassifyRuntimeStateField(RuntimeStateField::PersistedLayerStack),
			"persisted layer stack",
			"LayerStackStore",
			"Durable layer order, ids, shader selections, and bypass flags"
		},
		{
			RuntimeStateField::PersistedParameterValues,
			ClassifyRuntimeStateField(RuntimeStateField::PersistedParameterValues),
			"persisted parameter values",
			"LayerStackStore",
			"Saved parameter values used as the baseline for snapshots and presets"
		},
		{
			RuntimeStateField::StackPresets,
			ClassifyRuntimeStateField(RuntimeStateField::StackPresets),
			"stack presets",
			"RuntimeStore / LayerStackStore",
			"Durable preset files and preset serialization shape"
		},
		{
			RuntimeStateField::CommittedSessionParameterValues,
			ClassifyRuntimeStateField(RuntimeStateField::CommittedSessionParameterValues),
			"committed session parameter values",
			"RuntimeCoordinator policy, CommittedLiveState backing",
			"Operator/API truth after accepted mutations"
		},
		{
			RuntimeStateField::CommittedLayerBypass,
			ClassifyRuntimeStateField(RuntimeStateField::CommittedLayerBypass),
			"committed layer bypass",
			"RuntimeCoordinator policy, CommittedLiveState backing",
			"Current operator/API bypass state"
		},
		{
			RuntimeStateField::RuntimeCompileReloadFlags,
			ClassifyRuntimeStateField(RuntimeStateField::RuntimeCompileReloadFlags),
			"runtime compile/reload flags",
			"RuntimeCoordinator / RuntimeUpdateController",
			"Session coordination state used to request snapshot or render rebuild work"
		},
		{
			RuntimeStateField::TransientOscOverlay,
			ClassifyRuntimeStateField(RuntimeStateField::TransientOscOverlay),
			"transient OSC overlays",
			"RuntimeLiveState",
			"High-rate automation values applied above committed state"
		},
		{
			RuntimeStateField::TransientAutomationCommitState,
			ClassifyRuntimeStateField(RuntimeStateField::TransientAutomationCommitState),
			"transient automation commit state",
			"RuntimeLiveState / RuntimeServiceLiveBridge",
			"Generation and completion bookkeeping for settled overlay commits"
		},
		{
			RuntimeStateField::RenderLocalTemporalHistory,
			ClassifyRuntimeStateField(RuntimeStateField::RenderLocalTemporalHistory),
			"render-local temporal history",
			"RenderEngine",
			"GL/resource history that must stay out of parameter layering"
		},
		{
			RuntimeStateField::RenderLocalFeedbackState,
			ClassifyRuntimeStateField(RuntimeStateField::RenderLocalFeedbackState),
			"render-local feedback state",
			"RenderEngine",
			"Feedback buffers and ping-pong resources"
		},
		{
			RuntimeStateField::RenderLocalInputFrames,
			ClassifyRuntimeStateField(RuntimeStateField::RenderLocalInputFrames),
			"render-local input frames",
			"RenderEngine",
			"Latest accepted input frame payloads and upload staging"
		},
		{
			RuntimeStateField::RenderLocalOutputFrames,
			ClassifyRuntimeStateField(RuntimeStateField::RenderLocalOutputFrames),
			"render-local output frames",
			"RenderEngine",
			"Readback, packed output, screenshot, and preview staging"
		},
		{
			RuntimeStateField::RuntimeConfiguration,
			ClassifyRuntimeStateField(RuntimeStateField::RuntimeConfiguration),
			"runtime configuration",
			"RuntimeConfigStore",
			"File-backed config, not a live parameter layer"
		},
		{
			RuntimeStateField::HealthTelemetry,
			ClassifyRuntimeStateField(RuntimeStateField::HealthTelemetry),
			"health telemetry",
			"HealthTelemetry",
			"Operational observations, not source state for render values"
		}
	};
}
