package plan

import "time"

const (
	PlanStateActive   = "active"
	PlanStateArchived = "archived"
	ActorTypeHuman    = "human"
	ActorTypeAgent    = "agent"
)

// Actor is durable authoring provenance. It does not grant execution authority.
type Actor struct {
	Type     string
	ID       string
	MemberID string
}

// VersionRef identifies an immutable version selected by a Plan role.
type VersionRef struct {
	ID string
}

// Plan is workspace-scoped authoring metadata. Revision is the CAS token for
// metadata, lifecycle, and current/default-version pointer changes.
type Plan struct {
	ID                   string
	WorkspaceID          string
	ScopeID              string
	PlanKey              string
	DisplayName          string
	DescriptionMD        string
	State                string
	Revision             int64
	CurrentVersion       *VersionRef
	DefaultVersion       *VersionRef
	CreateIdempotencyKey string
	CreateRequestDigest  string
	CreatedBy            Actor
	CreatedAt            time.Time
	UpdatedBy            Actor
	UpdatedAt            time.Time
	ArchivedBy           *Actor
	ArchivedAt           *time.Time
}

// PlanDraft is the one mutable server-side draft for a Plan. Source and
// Layout have passed the complete authoring validation boundary before write.
type PlanDraft struct {
	PlanID      string
	WorkspaceID string
	Source      string
	Layout      []byte
	Revision    int64
	UpdatedBy   Actor
	CreatedAt   time.Time
	UpdatedAt   time.Time
}

// PlanVersion is an immutable published snapshot. DraftRevision is populated
// for draft publishes and nil for a historical republish; SourceVersionID is
// the inverse origin marker for a republish.
type PlanVersion struct {
	ID                     string
	WorkspaceID            string
	PlanID                 string
	DraftRevision          *int64
	SourceVersionID        *string
	Source                 string
	Layout                 []byte
	Artifact               []byte
	CompilerVersion        string
	Digest                 string
	PublishIdempotencyKey  string
	PublishRequestDigest   string
	PublishedBy            Actor
	PublishedByDisplayName string
	PublishedAt            time.Time
	CreatedAt              time.Time
}

// PlanVersionSummary is the lightweight immutable metadata projection used
// for version history pages. Large authoring and compiler payloads live only
// on PlanVersion and are loaded by exact-version operations.
type PlanVersionSummary struct {
	ID                     string
	WorkspaceID            string
	PlanID                 string
	DraftRevision          *int64
	SourceVersionID        *string
	CompilerVersion        string
	Digest                 string
	PublishedBy            Actor
	PublishedByDisplayName string
	PublishedAt            time.Time
}

// BindingVersionInspection is an exact immutable version plus the current
// Plan lifecycle observed while inspecting a proposed Binding revision.
// Archived versions remain inspectable for idempotent request replay; a new
// deployment must still use ResolveVersionForBinding or recheck active state in
// its own write transaction.
type BindingVersionInspection struct {
	PlanState string
	Version   PlanVersion
}

type CreateResult struct {
	Plan     Plan
	Draft    PlanDraft
	Replayed bool
}

type PublishResult struct {
	Plan     Plan
	Version  PlanVersion
	Replayed bool
}
