package domain

import "time"

// QueuedCommandID is a strongly-typed identifier for queued commands.
type QueuedCommandID string

// QueueStatus represents the status of a queued command.
type QueueStatus string

// Available queue statuses.
const (
	QueueStatusPending   QueueStatus = "pending"
	QueueStatusRunning   QueueStatus = "running"
	QueueStatusCompleted QueueStatus = "completed"
	QueueStatusFailed    QueueStatus = "failed"
	QueueStatusCancelled QueueStatus = "cancelled"
)

// IsTerminal returns true if the status represents a final state.
func (s QueueStatus) IsTerminal() bool {
	return s == QueueStatusCompleted || s == QueueStatusFailed || s == QueueStatusCancelled
}

// String returns the status as a string.
func (s QueueStatus) String() string {
	return string(s)
}

// QueuedCommand represents a command waiting to be executed.
type QueuedCommand struct {
	ID          QueuedCommandID `json:"id"`
	ProjectID   string          `json:"project_id"`
	Command     string          `json:"command"`      // Prompt for claude, command for shell, JSON-encoded args for git
	CommandType CommandType     `json:"command_type"` // claude, shell, git
	WorkingDir  string          `json:"working_dir,omitempty"`
	Status      QueueStatus     `json:"status"`
	Priority    int             `json:"priority"`
	CreatedAt   time.Time       `json:"created_at"`
	StartedAt   *time.Time      `json:"started_at,omitempty"`
	CompletedAt *time.Time      `json:"completed_at,omitempty"`
	ExitCode    *int            `json:"exit_code,omitempty"`
	Output      string          `json:"output,omitempty"`
	Error       string          `json:"error,omitempty"`
	APIKeyID    string          `json:"api_key_id,omitempty"` // For audit trail
}

// CommandResult holds the result of executing a queued command.
type QueuedCommandResult struct {
	ExitCode int
	Output   string
	Error    string
}

// QueueFilters contains filter options for listing queued commands.
type QueueFilters struct {
	Status    *QueueStatus // Filter by status
	Limit     int          // Max results (default 100)
	Offset    int          // For pagination
	SortOrder string       // "asc" or "desc" by created_at (default "desc")
}

// DefaultQueueFilters returns sensible defaults for queue listing.
func DefaultQueueFilters() *QueueFilters {
	return &QueueFilters{
		Limit:     100,
		Offset:    0,
		SortOrder: "desc",
	}
}

// QueueStats holds statistics about the command queue.
type QueueStats struct {
	TotalPending   int `json:"total_pending"`
	TotalRunning   int `json:"total_running"`
	TotalCompleted int `json:"total_completed"`
	TotalFailed    int `json:"total_failed"`
	TotalCancelled int `json:"total_cancelled"`
}
