package graphbuilder

import (
	"context"
	"io"
	"time"

	"github.com/tidbcloud/AgentTeam/kits/agentteam-insight/internal/graphbuilder/agent"
	"github.com/tidbcloud/AgentTeam/kits/agentteam-insight/internal/graphbuilder/semantic"
)

const (
	defaultOpenCodeBinary = "opencode"
	defaultMode           = ModeAuto
	defaultLLMMode        = LLMModeAuto
)

// Mode selects the high-level graph generation strategy.
type Mode string

type LLMMode string

const (
	LLMModeAuto LLMMode = "auto"
	LLMModeOff  LLMMode = "off"
)

const (
	ModeFull        Mode = "full"
	ModeIncremental Mode = "incremental"
	ModeAuto        Mode = "auto"
)

// Options describes one AgentTeam Insight graph builder run.
type Options struct {
	InputRepo              string
	OutputRoot             string
	Mode                   Mode
	LLMMode                LLMMode
	Jobs                   int
	OpenCodeBinary         string
	OpenCodeAgent          string
	OpenCodeModel          string
	OutputLanguage         string
	AutoUpdate             *bool
	NoPrompt               bool
	OpenCodeExtraArgs      []string
	Review                 bool
	IsolateOpenCodeRuntime bool
	TaskTimeout            time.Duration
	ProgressWriter         io.Writer
	BenchmarkPath          string
	PromptReader           io.Reader
	AgentRunner            agent.Runner
	// SemanticRunner overrides the architecture/tour LLM transport in tests.
	// Production leaves it nil so Run reuses the OpenCode CLI runner.
	SemanticRunner semantic.Runner
	benchmark      *benchmarkRecorder
}

// Result describes the graph output locations and deterministic run summary.
type Result struct {
	Mode          Mode
	Action        string
	InputRepo     string
	OutputRoot    string
	GraphDir      string
	GraphPath     string
	AnalyzedFiles int
	Nodes         int
	Edges         int
	Layers        int
	TourSteps     int
	TasksRun      int
	AgentEnhanced bool
}

// Command is the external process request issued by later graph builder stages.
type Command struct {
	Dir          string
	Name         string
	Args         []string
	Env          []string
	OutputWriter io.Writer
}

// CommandResult is the captured process output.
type CommandResult struct {
	Stdout []byte
	Stderr []byte
}

// CommandRunner runs an external command.
type CommandRunner func(context.Context, Command) (CommandResult, error)
