package port

import (
	"context"

	"github.com/orchard9/rdev/internal/sdlc"
)

// SDLCExecutor defines operations for executing SDLC commands in project pods.
// The adapter runs `sdlc` CLI commands via kubectl exec and parses JSON output.
type SDLCExecutor interface {
	// GetState returns the global SDLC state for a project pod.
	GetState(ctx context.Context, podName string) (*sdlc.State, error)

	// GetNext returns the classifier's recommendation for the next action.
	// If feature is empty, the classifier picks the most relevant feature.
	GetNext(ctx context.Context, podName, feature string) (*sdlc.Classification, error)

	// ListFeatures returns all features in the project.
	ListFeatures(ctx context.Context, podName string) ([]*sdlc.Feature, error)

	// GetFeature returns a single feature by slug.
	GetFeature(ctx context.Context, podName, slug string) (*sdlc.Feature, error)

	// CreateFeature creates a new feature with the given slug and title.
	CreateFeature(ctx context.Context, podName, slug, title string) (*sdlc.Feature, error)

	// TransitionFeature moves a feature to the specified phase.
	TransitionFeature(ctx context.Context, podName, slug string, phase sdlc.FeaturePhase) error

	// BlockFeature adds a blocker reason to a feature.
	BlockFeature(ctx context.Context, podName, slug, reason string) error

	// UnblockFeature removes all blockers from a feature.
	UnblockFeature(ctx context.Context, podName, slug string) error

	// DeleteFeature removes a feature entirely.
	DeleteFeature(ctx context.Context, podName, slug string) error

	// GetArtifactStatus returns artifact statuses for a feature.
	GetArtifactStatus(ctx context.Context, podName, slug string) (map[sdlc.ArtifactType]*sdlc.Artifact, error)

	// ApproveArtifact approves a feature artifact.
	ApproveArtifact(ctx context.Context, podName, slug string, artType sdlc.ArtifactType) error

	// RejectArtifact rejects a feature artifact.
	RejectArtifact(ctx context.Context, podName, slug string, artType sdlc.ArtifactType) error

	// PassArtifact marks a feature artifact as passed (for execution artifacts).
	PassArtifact(ctx context.Context, podName, slug string, artType sdlc.ArtifactType) error

	// FailArtifact marks a feature artifact as failed (for execution artifacts).
	FailArtifact(ctx context.Context, podName, slug string, artType sdlc.ArtifactType) error

	// NeedsFixArtifact marks a feature artifact as needing fixes.
	NeedsFixArtifact(ctx context.Context, podName, slug string, artType sdlc.ArtifactType) error

	// ListTasks returns all tasks for a feature.
	ListTasks(ctx context.Context, podName, slug string) ([]sdlc.Task, error)

	// AddTask adds a new task to a feature.
	AddTask(ctx context.Context, podName, slug, title string) (*sdlc.Task, error)

	// StartTask marks a task as in-progress.
	StartTask(ctx context.Context, podName, slug, taskID string) error

	// CompleteTask marks a task as complete.
	CompleteTask(ctx context.Context, podName, slug, taskID string) error

	// BlockTask marks a task as blocked.
	BlockTask(ctx context.Context, podName, slug, taskID string) error

	// QueryBlocked returns all blocked features.
	QueryBlocked(ctx context.Context, podName string) ([]BlockedInfo, error)

	// QueryReady returns features ready for work.
	QueryReady(ctx context.Context, podName string) ([]ReadyInfo, error)

	// QueryNeedsApproval returns features awaiting approval.
	QueryNeedsApproval(ctx context.Context, podName string) ([]ApprovalInfo, error)

	// CreateBranch creates a feature branch and its manifest.
	CreateBranch(ctx context.Context, podName, slug string) (*sdlc.BranchManifest, error)

	// GetBranchStatus returns the branch manifest, merge checklist, and readiness.
	GetBranchStatus(ctx context.Context, podName, slug string) (*BranchStatus, error)

	// SyncBranch syncs a feature branch with its base branch.
	SyncBranch(ctx context.Context, podName, slug string) error

	// MergeFeature merges a feature branch after all gates pass.
	MergeFeature(ctx context.Context, podName, slug, strategy string) error

	// ArchiveFeature archives a released feature.
	ArchiveFeature(ctx context.Context, podName, slug string) error
}

// BlockedInfo describes a blocked feature (matches sdlc query --json output).
type BlockedInfo struct {
	Slug     string   `json:"slug"`
	Phase    string   `json:"phase"`
	Blockers []string `json:"blockers"`
}

// ReadyInfo describes a feature ready for work (matches sdlc query --json output).
type ReadyInfo struct {
	Slug   string `json:"slug"`
	Phase  string `json:"phase"`
	Action string `json:"action"`
}

// ApprovalInfo describes a feature awaiting approval (matches sdlc query --json output).
type ApprovalInfo struct {
	Slug    string `json:"slug"`
	Phase   string `json:"phase"`
	Message string `json:"message"`
}

// BranchStatus contains the full branch status response including checklist.
type BranchStatus struct {
	Branch    *sdlc.BranchManifest `json:"branch"`
	Checklist []string             `json:"checklist"`
	Ready     bool                 `json:"ready"`
}
