package com.optix.android.domain.model

/**
 * A single step in a saved workflow.
 *
 * [toolSlug] is the ToolRegistry key (ToolMetadata.name). [params] are serialised
 * as strings so they can be persisted and replayed through the existing tool-run path.
 */
data class WorkflowStep(
    val toolSlug: String,
    val params: Map<String, String> = emptyMap(),
)

/**
 * A reusable, ordered chain of tool steps the user can save and run later.
 */
data class Workflow(
    val id: Long = 0,
    val name: String,
    val steps: List<WorkflowStep> = emptyList(),
    val createdAt: Long = System.currentTimeMillis(),
)

// ---------------------------------------------------------------------------
// AI-orchestrated chain aliases
//
// [ToolChainStep] / [ToolChain] are the runtime equivalents of
// [WorkflowStep] / [Workflow] produced when the AI assistant suggests a
// multi-step attack / workflow chain that should be executed in the NETWORK tab.
// They are kept as separate types so the AI orchestration layer does not depend
// on persistence concerns, but they map 1:1 to the Workflow model.
// ---------------------------------------------------------------------------

data class ToolChainStep(
    val toolSlug: String,
    val params: Map<String, String> = emptyMap(),
)

data class ToolChain(
    val steps: List<ToolChainStep>,
    val name: String = "AI Chain",
)

fun ToolChain.toWorkflow(name: String = this.name): Workflow =
    Workflow(
        name = name,
        steps = steps.map { WorkflowStep(toolSlug = it.toolSlug, params = it.params) },
    )

fun Workflow.toToolChain(): ToolChain =
    ToolChain(
        name = name,
        steps = steps.map { ToolChainStep(toolSlug = it.toolSlug, params = it.params) },
    )
