package domain

import "time"

// BuildEventType categorizes build streaming events.
type BuildEventType string

// Build event types for SSE streaming.
const (
	// BuildEventStarted is emitted when a build begins execution.
	BuildEventStarted BuildEventType = "build.started"

	// BuildEventOutput is emitted for each line of agent output (stdout/stderr).
	BuildEventOutput BuildEventType = "build.output"

	// BuildEventToolUse is emitted when the agent invokes a tool.
	BuildEventToolUse BuildEventType = "build.tool_use"

	// BuildEventToolResult is emitted when a tool returns a result.
	BuildEventToolResult BuildEventType = "build.tool_result"

	// BuildEventProgress is emitted periodically with progress estimates.
	BuildEventProgress BuildEventType = "build.progress"

	// BuildEventError is emitted for error output during execution.
	BuildEventError BuildEventType = "build.error"

	// BuildEventCompleted is emitted when a build finishes successfully.
	BuildEventCompleted BuildEventType = "build.completed"

	// BuildEventFailed is emitted when a build fails.
	BuildEventFailed BuildEventType = "build.failed"
)

// BuildEvent represents a single event in a build's execution stream.
// Events are published to SSE subscribers in real-time and persisted for replay.
type BuildEvent struct {
	// ID is the unique event identifier (format: "{taskID}:{sequence}").
	ID string `json:"id"`

	// TaskID links this event to a build task.
	TaskID string `json:"task_id"`

	// ProjectID links this event to a project.
	ProjectID string `json:"project_id"`

	// Type categorizes this event.
	Type BuildEventType `json:"type"`

	// Timestamp when the event occurred.
	Timestamp time.Time `json:"timestamp"`

	// Sequence is the monotonically increasing event number within a task.
	Sequence int64 `json:"sequence"`

	// Data contains event-type-specific payload.
	Data BuildEventData `json:"data"`
}

// BuildEventData holds the payload for different event types.
type BuildEventData struct {
	// Content is the main text content (output lines, error messages).
	Content string `json:"content,omitempty"`

	// Stream identifies the output source ("stdout", "stderr").
	Stream string `json:"stream,omitempty"`

	// ToolName is set for tool_use and tool_result events.
	ToolName string `json:"tool_name,omitempty"`

	// Progress fields (for build.progress events)
	Phase      string  `json:"phase,omitempty"`      // Current phase: "starting", "reading", "writing", "testing", "committing"
	Percentage float64 `json:"percentage,omitempty"` // Estimated completion percentage (0-100)

	// Completion fields (for build.completed and build.failed events)
	Success      bool     `json:"success,omitempty"`
	Error        string   `json:"error,omitempty"`
	CommitSHA    string   `json:"commit_sha,omitempty"`
	FilesChanged []string `json:"files_changed,omitempty"`
	DurationMs   int64    `json:"duration_ms,omitempty"`
}

// BuildPhase represents the current phase of a build.
type BuildPhase string

// Build phases for progress tracking.
const (
	BuildPhaseStarting   BuildPhase = "starting"
	BuildPhaseReading    BuildPhase = "reading"
	BuildPhaseWriting    BuildPhase = "writing"
	BuildPhaseTesting    BuildPhase = "testing"
	BuildPhaseCommitting BuildPhase = "committing"
	BuildPhaseComplete   BuildPhase = "complete"
)

// PhaseWeight returns the progress weight for a phase (used for percentage estimation).
func (p BuildPhase) Weight() float64 {
	switch p {
	case BuildPhaseStarting:
		return 0.05
	case BuildPhaseReading:
		return 0.15
	case BuildPhaseWriting:
		return 0.50
	case BuildPhaseTesting:
		return 0.20
	case BuildPhaseCommitting:
		return 0.10
	default:
		return 0
	}
}
