package sdlc

import "time"

// Artifact tracks the status of a feature artifact.
type Artifact struct {
	Status     ArtifactStatus `yaml:"status" json:"status"`
	Path       string         `yaml:"path" json:"path"`
	ApprovedBy string         `yaml:"approved_by,omitempty" json:"approved_by,omitempty"`
	ApprovedAt *time.Time     `yaml:"approved_at,omitempty" json:"approved_at,omitempty"`
	RejectedBy string         `yaml:"rejected_by,omitempty" json:"rejected_by,omitempty"`
	RejectedAt *time.Time     `yaml:"rejected_at,omitempty" json:"rejected_at,omitempty"`
	Total      int            `yaml:"total,omitempty" json:"total,omitempty"`
	Completed  int            `yaml:"completed,omitempty" json:"completed,omitempty"`
	InProgress int            `yaml:"in_progress,omitempty" json:"in_progress,omitempty"`
	Blocked    int            `yaml:"blocked,omitempty" json:"blocked,omitempty"`
}

// NewArtifact creates an artifact in pending status.
func NewArtifact(artifactType ArtifactType) *Artifact {
	return &Artifact{
		Status: StatusPending,
		Path:   ArtifactFilename(artifactType),
	}
}

// Approve marks the artifact as approved.
func (a *Artifact) Approve(by string) {
	now := time.Now().UTC()
	a.Status = StatusApproved
	a.ApprovedBy = by
	a.ApprovedAt = &now
	a.RejectedBy = ""
	a.RejectedAt = nil
}

// Reject marks the artifact as rejected.
func (a *Artifact) Reject(by string) {
	now := time.Now().UTC()
	a.Status = StatusRejected
	a.RejectedBy = by
	a.RejectedAt = &now
}

// MarkDraft sets the artifact status to draft.
func (a *Artifact) MarkDraft() {
	a.Status = StatusDraft
}

// MarkPassed sets the artifact status to passed.
func (a *Artifact) MarkPassed() {
	a.Status = StatusPassed
}

// MarkFailed sets the artifact status to failed.
func (a *Artifact) MarkFailed() {
	a.Status = StatusFailed
}

// MarkNeedsFix sets the artifact status to needs_fix.
func (a *Artifact) MarkNeedsFix() {
	a.Status = StatusNeedsFix
}
