package sdlc

import (
	"slices"
	"time"
)

// FeaturePhase represents a stage in the feature lifecycle.
type FeaturePhase string

const (
	PhaseDraft          FeaturePhase = "draft"
	PhaseSpecified      FeaturePhase = "specified"
	PhasePlanned        FeaturePhase = "planned"
	PhaseReady          FeaturePhase = "ready"
	PhaseImplementation FeaturePhase = "implementation"
	PhaseReview         FeaturePhase = "review"
	PhaseAudit          FeaturePhase = "audit"
	PhaseQA             FeaturePhase = "qa"
	PhaseMerge          FeaturePhase = "merge"
	PhaseReleased       FeaturePhase = "released"
)

// ValidPhases is the ordered list of all feature phases.
var ValidPhases = []FeaturePhase{
	PhaseDraft,
	PhaseSpecified,
	PhasePlanned,
	PhaseReady,
	PhaseImplementation,
	PhaseReview,
	PhaseAudit,
	PhaseQA,
	PhaseMerge,
	PhaseReleased,
}

// PhaseIndex returns the ordinal position of a phase, or -1 if invalid.
func PhaseIndex(p FeaturePhase) int {
	for i, v := range ValidPhases {
		if v == p {
			return i
		}
	}
	return -1
}

// IsValidPhase returns true if the phase is recognized.
func IsValidPhase(p FeaturePhase) bool {
	return PhaseIndex(p) >= 0
}

// ArtifactType identifies the kind of artifact produced during a phase.
type ArtifactType string

const (
	ArtifactSpec      ArtifactType = "spec"
	ArtifactDesign    ArtifactType = "design"
	ArtifactTasks     ArtifactType = "tasks"
	ArtifactQAPlan    ArtifactType = "qa_plan"
	ArtifactReview    ArtifactType = "review"
	ArtifactAudit     ArtifactType = "audit"
	ArtifactQAResults ArtifactType = "qa_results"
)

// ValidArtifactTypes lists all recognized artifact types.
var ValidArtifactTypes = []ArtifactType{
	ArtifactSpec,
	ArtifactDesign,
	ArtifactTasks,
	ArtifactQAPlan,
	ArtifactReview,
	ArtifactAudit,
	ArtifactQAResults,
}

// IsValidArtifactType returns true if the artifact type is recognized.
func IsValidArtifactType(t ArtifactType) bool {
	return slices.Contains(ValidArtifactTypes, t)
}

// ArtifactFilename returns the file name for an artifact type.
func ArtifactFilename(t ArtifactType) string {
	switch t {
	case ArtifactSpec:
		return "spec.md"
	case ArtifactDesign:
		return "design.md"
	case ArtifactTasks:
		return "tasks.md"
	case ArtifactQAPlan:
		return "qa-plan.md"
	case ArtifactReview:
		return "review.md"
	case ArtifactAudit:
		return "audit.md"
	case ArtifactQAResults:
		return "qa-results.md"
	default:
		return ""
	}
}

// ArtifactStatus tracks the approval state of an artifact.
type ArtifactStatus string

const (
	StatusPending  ArtifactStatus = "pending"
	StatusDraft    ArtifactStatus = "draft"
	StatusApproved ArtifactStatus = "approved"
	StatusRejected ArtifactStatus = "rejected"
	StatusNeedsFix ArtifactStatus = "needs_fix"
	StatusPassed   ArtifactStatus = "passed"
	StatusFailed   ArtifactStatus = "failed"
)

// ActionType identifies what the classifier recommends.
type ActionType string

const (
	ActionBlocked         ActionType = "BLOCKED"
	ActionAwaitApproval   ActionType = "AWAIT_APPROVAL"
	ActionTransition      ActionType = "TRANSITION"
	ActionCreateSpec      ActionType = "CREATE_SPEC"
	ActionCreateDesign    ActionType = "CREATE_DESIGN"
	ActionCreateTasks     ActionType = "CREATE_TASKS"
	ActionCreateQAPlan    ActionType = "CREATE_QA_PLAN"
	ActionCreateBranch    ActionType = "CREATE_BRANCH"
	ActionImplementTask   ActionType = "IMPLEMENT_TASK"
	ActionReviewCode      ActionType = "REVIEW_CODE"
	ActionFixReviewIssues ActionType = "FIX_REVIEW_ISSUES"
	ActionAuditCode       ActionType = "AUDIT_CODE"
	ActionRemediateAudit  ActionType = "REMEDIATE_AUDIT"
	ActionRunQA           ActionType = "RUN_QA"
	ActionFixQAFailures   ActionType = "FIX_QA_FAILURES"
	ActionMergeFeature    ActionType = "MERGE_FEATURE"
	ActionArchive         ActionType = "ARCHIVE"
	ActionIdle            ActionType = "IDLE"
)

// TaskStatus tracks the state of an implementation task.
type TaskStatus string

const (
	TaskPending    TaskStatus = "pending"
	TaskInProgress TaskStatus = "in_progress"
	TaskComplete   TaskStatus = "complete"
	TaskBlocked    TaskStatus = "blocked"
)

// PhaseTransition records when a feature moved between phases.
type PhaseTransition struct {
	Phase   FeaturePhase `yaml:"phase" json:"phase"`
	Entered time.Time    `yaml:"entered" json:"entered"`
	Exited  *time.Time   `yaml:"exited,omitempty" json:"exited,omitempty"`
}

// Dependencies tracks what a feature depends on.
type Dependencies struct {
	Features []string `yaml:"features,omitempty" json:"features,omitempty"`
	Patterns []string `yaml:"patterns,omitempty" json:"patterns,omitempty"`
}
