package checkpoint

import "time"

const (
	WorkspaceSchemaVersion   = "notarius.workspace.v4"
	WorkspaceSchemaVersionV3 = "notarius.workspace.v3"
	WorkspaceSchemaVersionV2 = "notarius.workspace.v2"
	WorkspaceSchemaVersionV1 = "notarius.workspace.v1"
)

type StageName string

const (
	StageSource    StageName = "source"
	StageExtract   StageName = "extract"
	StageMerge     StageName = "merge"
	StageNormalize StageName = "normalize"
)

type StageStatus string

const (
	StatusPending                 StageStatus = "pending"
	StatusRunning                 StageStatus = "running"
	StatusSucceeded               StageStatus = "succeeded"
	StatusSucceededWithRejections StageStatus = "succeeded_with_rejections"
	StatusFailed                  StageStatus = "failed"
	StatusInvalidated             StageStatus = "invalidated"
)

type StageManifest struct {
	WorkspaceSchemaVersion string             `json:"workspace_schema_version"`
	Stage                  StageName          `json:"stage"`
	StepID                 string             `json:"step_id,omitempty"`
	LaneID                 string             `json:"lane_id,omitempty"`
	ModuleKey              string             `json:"module_key,omitempty"`
	DependencyFingerprints []Fingerprint      `json:"dependency_fingerprints,omitempty"`
	Status                 StageStatus        `json:"status"`
	OutputDigests          []Fingerprint      `json:"output_digests,omitempty"`
	ValidationStatus       string             `json:"validation_status,omitempty"`
	Rejections             []RejectionSummary `json:"rejections,omitempty"`
	StartedAt              *time.Time         `json:"started_at,omitempty"`
	CompletedAt            *time.Time         `json:"completed_at,omitempty"`
	Metadata               map[string]string  `json:"metadata,omitempty"`
}
type RejectionSummary struct {
	ValidatorName string `json:"validator_name,omitempty"`
	ReasonCode    string `json:"reason_code,omitempty"`
	Message       string `json:"message,omitempty"`
	Count         int    `json:"count,omitempty"`
}
type SourceManifest struct {
	StageManifest
	SourceID string `json:"source_id,omitempty"`
}
type ExtractLaneManifest struct {
	StageManifest
	ChunkCount  int `json:"chunk_count,omitempty"`
	OutputCount int `json:"output_count,omitempty"`
}
type MergeLaneManifest struct {
	StageManifest
	InputCount int `json:"input_count,omitempty"`
}
type NormalizeLaneManifest struct {
	StageManifest
	InputCount int `json:"input_count,omitempty"`
}

func NewStageManifest(stage StageName, status StageStatus) StageManifest {
	return StageManifest{WorkspaceSchemaVersion: WorkspaceSchemaVersion, Stage: stage, Status: status}
}
