package routing

import "time"

// WorkflowSpec is generated by llm-router (one-time execution)
type WorkflowSpec struct {
	Name   string                 `json:"name"`
	Input  map[string]interface{} `json:"input"`
	States []State                `json:"states"`
}

// CronWorkflowSpec is generated by llm-router (scheduled execution)
type CronWorkflowSpec struct {
	Name          string                 `json:"name"`
	Type          string                 `json:"type"`         // "CronWorkflow"
	Schedule      string                 `json:"schedule"`     // Cron expression (e.g., "0 2 * * *")
	Timezone      string                 `json:"timezone"`     // "UTC", "America/New_York", etc
	Input         map[string]interface{} `json:"input"`        // Fixed input for each run
	States        []State                `json:"states"`       // Workflow states
	MaxConcurrent int                    `json:"maxConcurrent,omitempty"`   // Max parallel runs (default 1)
	Timeout       string                 `json:"timeout,omitempty"`         // Overall timeout per run
	EnableHistory bool                   `json:"enableHistory,omitempty"`   // Keep execution history
}

// State is a step in the workflow
type State struct {
	Name              string
	Type              StateType `json:"type"`
	
	// Task fields
	Resource          string                 `json:"resource,omitempty"`
	Parameters        map[string]interface{} `json:"parameters,omitempty"`
	Timeout           string                 `json:"timeout,omitempty"`
	Retry             *RetryPolicy           `json:"retry,omitempty"`
	Catch             []CatchClause          `json:"catch,omitempty"`
	
	// Pass fields
	Result            interface{} `json:"result,omitempty"`
	
	// Fail fields
	Error             string `json:"error,omitempty"`
	Cause             string `json:"cause,omitempty"`
	
	// Transition
	Next              string `json:"next,omitempty"`
	End               bool   `json:"end,omitempty"`
}

// StateType defines valid state types
type StateType string

const (
	StateTypeTask StateType = "Task"
	StateTypePass StateType = "Pass"
	StateTypeFail StateType = "Fail"
)

// RetryPolicy defines retry behavior for activities
type RetryPolicy struct {
	MaxAttempts     int32   `json:"maxAttempts"`
	BackoffRate     float64 `json:"backoffRate"`
	InitialInterval string  `json:"initialInterval"`
	MaxInterval     string  `json:"maxInterval,omitempty"`
}

// CatchClause defines error handling
type CatchClause struct {
	ErrorEquals []string `json:"errorEquals"`
	ResultPath  *string  `json:"resultPath,omitempty"`
	Next        string   `json:"next"`
}

// ExecutionContext tracks state during workflow execution
type ExecutionContext struct {
	Input       map[string]interface{}
	StepResults map[string]interface{}
	CurrentState string
	History     []ExecutionEvent
}

// ExecutionEvent tracks individual state execution
type ExecutionEvent struct {
	Timestamp time.Time
	State     string
	Type      string // "Started", "Completed", "Failed", "Retried"
	Result    interface{}
	Error     error
}

// PollParams for AwaitTaskComplete states
type PollParams struct {
	QueueName     string
	CorrelationID string
	PollInterval  time.Duration
	Timeout       time.Duration
}

// PollResult is the result of polling
type PollResult struct {
	Result interface{}
	Status string
}

// Result is the final workflow output
type Result struct {
	FinalOutput interface{}
	Status      string // "COMPLETED", "FAILED"
	Error       error
}

// Heartbeat contains state for polling activities
type Heartbeat struct {
	CorrelationID string
	Queue         string
	Attempt       int
	Elapsed       time.Duration
	LastCheck     time.Time
}

// ActivityMetadata describes an activity's capabilities and constraints
type ActivityMetadata struct {
	Name        string
	Description string
	Category    string
	Inputs      map[string]InputField
	Outputs     map[string]OutputField
	Constraints Constraints
}

// InputField describes an activity input parameter
type InputField struct {
	Type        string      `json:"type"`
	Description string      `json:"description"`
	Required    bool        `json:"required"`
	Default     interface{} `json:"default,omitempty"`
}

// OutputField describes an activity output field
type OutputField struct {
	Type        string `json:"type"`
	Description string `json:"description"`
}

// Constraints describes activity execution constraints
type Constraints struct {
	DefaultTimeout    string
	IsFlaky           bool
	RecommendedRetries int
	RetryBackoff      float64
	Dependencies      []string
	Notes             string
}
