package pipeline

import (
	"time"

	"gitea.maximumdirect.net/eric/notarius/internal/core/artifacts"
	"gitea.maximumdirect.net/eric/notarius/internal/core/source"
	"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
)

const ChunkPlanSchemaVersion = "notarius.chunk-plan.v3"

type ChunkPlanProducer struct {
	InputModule string                          `json:"input_module"`
	ChunkModule string                          `json:"chunk_module"`
	LLMProfile  string                          `json:"llm_profile,omitempty"`
	References  []artifacts.ReferenceProvenance `json:"references,omitempty"`
	Metadata    map[string]any                  `json:"metadata,omitempty"`
}

type ChunkPlanRecord struct {
	SchemaVersion string                         `json:"schema_version"`
	SourceDigest  string                         `json:"source_digest"`
	PlanDigest    string                         `json:"plan_digest"`
	Plan          source.ChunkPlan               `json:"plan"`
	Producer      ChunkPlanProducer              `json:"producer"`
	Diagnostics   []contracts.ProducerDiagnostic `json:"diagnostics,omitempty"`
	CreatedAt     time.Time                      `json:"created_at"`
}

type ChunkPlanStore interface {
	Load(sourceDigest string) (ChunkPlanRecord, ChunkPlanDecision, error)
	Save(ChunkPlanRecord) error
}

type ChunkPlanStoreFactory func(root string) (ChunkPlanStore, error)

type ChunkPlanDecision struct {
	Status ChunkPlanStatus `json:"status"`
	Reason string          `json:"reason,omitempty"`
}

type ChunkPlanStatus string

const (
	ChunkPlanHit     ChunkPlanStatus = "hit"
	ChunkPlanMissing ChunkPlanStatus = "missing"
	ChunkPlanInvalid ChunkPlanStatus = "invalid"
)
