package dto

import (
	"encoding/json"
	"time"
)

// WorkflowPlanCreateRequest creates Plan metadata, its canonical Workflow
// Scope, and its first fully compiled durable authoring draft.
type WorkflowPlanCreateRequest struct {
	Key           string          `json:"key"`
	DisplayName   string          `json:"displayName"`
	DescriptionMD string          `json:"descriptionMd,omitempty"`
	Source        *string         `json:"source"`
	Layout        json.RawMessage `json:"layout,omitempty"`
}

// WorkflowPlanUpdateRequest changes metadata under a Plan ETag fence.
type WorkflowPlanUpdateRequest struct {
	DisplayName   *string `json:"displayName,omitempty"`
	DescriptionMD *string `json:"descriptionMd,omitempty"`
}

// WorkflowPlanDraftSaveRequest replaces the server draft under its own ETag.
type WorkflowPlanDraftSaveRequest struct {
	Source *string         `json:"source"`
	Layout json.RawMessage `json:"layout,omitempty"`
}

// WorkflowPlanValidateRequest validates source without mutating a draft.
type WorkflowPlanValidateRequest struct {
	Source *string `json:"source"`
}

// WorkflowPlanDraftValidateRequest validates one exact revision of the
// server-side draft without changing the draft or Plan aggregate.
type WorkflowPlanDraftValidateRequest struct {
	DraftRevision int64 `json:"draftRevision"`
}

// WorkflowPlanPublishRequest seals one exact draft revision.
type WorkflowPlanPublishRequest struct {
	DraftRevision int64 `json:"draftRevision"`
	MakeDefault   bool  `json:"makeDefault,omitempty"`
}

// WorkflowPlanRepublishRequest appends a new immutable version from one
// historical version; the source version itself is never mutated.
type WorkflowPlanRepublishRequest struct {
	MakeDefault bool `json:"makeDefault,omitempty"`
}

// WorkflowPlanActorResponse is authoring provenance, not the execution owner
// used later by Binding.
type WorkflowPlanActorResponse struct {
	Type     string `json:"type"`
	ID       string `json:"id"`
	MemberID string `json:"memberId"`
}

type WorkflowPlanVersionRefResponse struct {
	ID string `json:"id"`
}

// WorkflowPlanManagementResponse identifies the domain that owns publication
// and lifecycle changes for a Workflow Plan. It is omitted for ordinary
// workspace Plans whose lifecycle remains managed by the Plan authoring API.
type WorkflowPlanManagementResponse struct {
	Kind           string `json:"kind"`
	TeamID         string `json:"teamId"`
	TeamWorkflowID string `json:"teamWorkflowId"`
	RelationState  string `json:"relationState"`
}

type WorkflowPlanResponse struct {
	ID             string                          `json:"id"`
	ScopeID        string                          `json:"scopeId"`
	Key            string                          `json:"key"`
	DisplayName    string                          `json:"displayName"`
	DescriptionMD  string                          `json:"descriptionMd"`
	State          string                          `json:"state"`
	Revision       int64                           `json:"revision"`
	CurrentVersion *WorkflowPlanVersionRefResponse `json:"currentVersion,omitempty"`
	DefaultVersion *WorkflowPlanVersionRefResponse `json:"defaultVersion,omitempty"`
	CreatedBy      WorkflowPlanActorResponse       `json:"createdBy"`
	CreatedAt      time.Time                       `json:"createdAt"`
	UpdatedBy      WorkflowPlanActorResponse       `json:"updatedBy"`
	UpdatedAt      time.Time                       `json:"updatedAt"`
	ArchivedBy     *WorkflowPlanActorResponse      `json:"archivedBy,omitempty"`
	ArchivedAt     *time.Time                      `json:"archivedAt,omitempty"`
	Management     *WorkflowPlanManagementResponse `json:"management,omitempty"`
	CanEdit        *bool                           `json:"canEdit,omitempty"`
}

type WorkflowPlanDraftResponse struct {
	PlanID    string                    `json:"planId"`
	Source    string                    `json:"source"`
	Layout    json.RawMessage           `json:"layout"`
	Revision  int64                     `json:"revision"`
	UpdatedBy WorkflowPlanActorResponse `json:"updatedBy"`
	CreatedAt time.Time                 `json:"createdAt"`
	UpdatedAt time.Time                 `json:"updatedAt"`
}

type WorkflowPlanVersionResponse struct {
	ID                     string                    `json:"id"`
	PlanID                 string                    `json:"planId"`
	DraftRevision          *int64                    `json:"draftRevision,omitempty"`
	SourceVersionID        *string                   `json:"sourceVersionId,omitempty"`
	Source                 string                    `json:"source"`
	Layout                 json.RawMessage           `json:"layout"`
	Artifact               json.RawMessage           `json:"artifact"`
	CompilerVersion        string                    `json:"compilerVersion"`
	Digest                 string                    `json:"digest"`
	PublishedBy            WorkflowPlanActorResponse `json:"publishedBy"`
	PublishedByDisplayName string                    `json:"publishedByDisplayName"`
	PublishedAt            time.Time                 `json:"publishedAt"`
}

// WorkflowPlanVersionSummaryResponse is returned by version history pages.
// Exact-version reads use WorkflowPlanVersionResponse and include the three
// potentially large immutable payloads.
type WorkflowPlanVersionSummaryResponse struct {
	ID                     string                    `json:"id"`
	PlanID                 string                    `json:"planId"`
	DraftRevision          *int64                    `json:"draftRevision,omitempty"`
	SourceVersionID        *string                   `json:"sourceVersionId,omitempty"`
	CompilerVersion        string                    `json:"compilerVersion"`
	Digest                 string                    `json:"digest"`
	PublishedBy            WorkflowPlanActorResponse `json:"publishedBy"`
	PublishedByDisplayName string                    `json:"publishedByDisplayName"`
	PublishedAt            time.Time                 `json:"publishedAt"`
}

type WorkflowPlanDiagnosticResponse struct {
	Severity string `json:"severity"`
	Code     string `json:"code"`
	Path     string `json:"path"`
	Message  string `json:"message"`
	Line     int    `json:"line,omitempty"`
	Column   int    `json:"column,omitempty"`
	NodeID   string `json:"nodeId,omitempty"`
	EdgeID   string `json:"edgeId,omitempty"`
}

type WorkflowPlanValidationResponse struct {
	Diagnostics []WorkflowPlanDiagnosticResponse `json:"diagnostics"`
	Artifact    json.RawMessage                  `json:"artifact,omitempty"`
	Digest      string                           `json:"digest,omitempty"`
}

type WorkflowPlanCreateResponse struct {
	Plan  WorkflowPlanResponse      `json:"plan"`
	Draft WorkflowPlanDraftResponse `json:"draft"`
}

// WorkflowPlanAgentViewResponse is the minimal read model an Agent needs to
// revise, publish, archive, or invoke a Plan. Source is YAML; layout and
// compiled artifact are intentionally absent.
type WorkflowPlanAgentViewResponse struct {
	PlanID          string                          `json:"planId"`
	ScopeID         string                          `json:"scopeId"`
	PlanRevision    int64                           `json:"planRevision"`
	State           string                          `json:"state"`
	Source          string                          `json:"source"`
	SourceCursor    string                          `json:"sourceCursor,omitempty"`
	WorkingRevision int64                           `json:"workingRevision"`
	PlanVersion     *WorkflowPlanVersionRefResponse `json:"planVersion,omitempty"`
	TeamID          string                          `json:"teamId,omitempty"`
	TeamWorkflowID  string                          `json:"teamWorkflowId,omitempty"`
	Payload         *WorkflowPayloadPageResponse    `json:"payload,omitempty"`
}

// Agent Workflow reads use explicit section/payload continuation rather than
// allowing a large source or layout to be semantically truncated by the Exec
// result budget.
type AgentWorkflowPlanShowInput struct {
	Action  string `json:"action"`
	PlanID  string `json:"planId"`
	Section string `json:"section,omitempty"`
	Path    string `json:"path,omitempty"`
	Cursor  string `json:"cursor,omitempty"`
	Limit   *int   `json:"limit,omitempty"`
}

type AgentWorkflowPlanVersionGetInput struct {
	Action    string `json:"action"`
	PlanID    string `json:"planId"`
	VersionID string `json:"versionId"`
	Section   string `json:"section,omitempty"`
	Path      string `json:"path,omitempty"`
	Cursor    string `json:"cursor,omitempty"`
	Limit     *int   `json:"limit,omitempty"`
}

type WorkflowPayloadPageResponse struct {
	Section    string `json:"section"`
	Encoding   string `json:"encoding"`
	Value      string `json:"value"`
	NextCursor string `json:"nextCursor,omitempty"`
}

type WorkflowPlanPublishResponse struct {
	Plan    WorkflowPlanResponse        `json:"plan"`
	Version WorkflowPlanVersionResponse `json:"version"`
}

type WorkflowPlanListResponse struct {
	Items      []WorkflowPlanResponse `json:"items"`
	NextCursor string                 `json:"nextCursor,omitempty"`
}

type WorkflowPlanVersionListResponse struct {
	Items      []WorkflowPlanVersionSummaryResponse `json:"items"`
	NextCursor string                               `json:"nextCursor,omitempty"`
}
