package dto

import (
	"encoding/json"
	"time"
)

// WorkflowBindingCreateRequest creates a Binding and its first immutable
// revision. activate is deliberately explicit: creating authoring state must
// not silently make it executable.
type WorkflowBindingCreateRequest struct {
	Key           string          `json:"key"`
	DisplayName   string          `json:"displayName"`
	DescriptionMD string          `json:"descriptionMd,omitempty"`
	PlanID        string          `json:"planId"`
	PlanVersionID string          `json:"planVersionId"`
	Source        json.RawMessage `json:"source"`
	InputPolicy   json.RawMessage `json:"inputPolicy,omitempty"`
	Activate      *bool           `json:"activate"`
}

// WorkflowBindingUpdateRequest changes only mutable Binding metadata. A new
// executable configuration is represented by a new Binding revision instead.
type WorkflowBindingUpdateRequest struct {
	DisplayName   *string `json:"displayName,omitempty"`
	DescriptionMD *string `json:"descriptionMd,omitempty"`
}

// WorkflowBindingValidateRequest validates a prospective immutable revision
// without creating or activating it.
type WorkflowBindingValidateRequest struct {
	PlanID        string          `json:"planId"`
	PlanVersionID string          `json:"planVersionId"`
	Source        json.RawMessage `json:"source"`
	InputPolicy   json.RawMessage `json:"inputPolicy,omitempty"`
}

// WorkflowBindingRevisionCreateRequest appends an immutable revision. It does
// not activate the revision; activation is a separate fenced operation.
type WorkflowBindingRevisionCreateRequest struct {
	PlanVersionID string          `json:"planVersionId"`
	Source        json.RawMessage `json:"source"`
	InputPolicy   json.RawMessage `json:"inputPolicy,omitempty"`
}

// WorkflowBindingActorResponse is provenance only. Agent tool schemas never
// accept this shape; human and Agent actors are derived from authenticated
// transport context.
type WorkflowBindingActorResponse struct {
	Type     string `json:"type"`
	ID       string `json:"id"`
	MemberID string `json:"memberId,omitempty"`
}

// WorkflowBindingOwnerResponse identifies the human execution authority held
// by a Binding. It is output-only on both human and Agent transports.
type WorkflowBindingOwnerResponse struct {
	UserID   string `json:"userId"`
	MemberID string `json:"memberId"`
}

type WorkflowBindingRevisionRefResponse struct {
	ID       string `json:"id"`
	Revision int64  `json:"revision"`
}

type WorkflowBindingResponse struct {
	ID              string                              `json:"id"`
	PlanID          string                              `json:"planId"`
	PlanDisplayName string                              `json:"planDisplayName,omitempty"`
	Key             string                              `json:"key"`
	DisplayName     string                              `json:"displayName"`
	DescriptionMD   string                              `json:"descriptionMd"`
	State           string                              `json:"state"`
	Revision        int64                               `json:"revision"`
	LatestRevision  *WorkflowBindingRevisionRefResponse `json:"latestRevision,omitempty"`
	ActiveRevision  *WorkflowBindingRevisionRefResponse `json:"activeRevision,omitempty"`
	Owner           WorkflowBindingOwnerResponse        `json:"owner"`
	CreatedBy       WorkflowBindingActorResponse        `json:"createdBy"`
	CreatedAt       time.Time                           `json:"createdAt"`
	UpdatedBy       WorkflowBindingActorResponse        `json:"updatedBy"`
	UpdatedAt       time.Time                           `json:"updatedAt"`
	ArchivedBy      *WorkflowBindingActorResponse       `json:"archivedBy,omitempty"`
	ArchivedAt      *time.Time                          `json:"archivedAt,omitempty"`
}

type WorkflowBindingRevisionResponse struct {
	ID            string                       `json:"id"`
	BindingID     string                       `json:"bindingId"`
	PlanID        string                       `json:"planId"`
	Revision      int64                        `json:"revision"`
	PlanVersionID string                       `json:"planVersionId"`
	Source        json.RawMessage              `json:"source"`
	InputPolicy   json.RawMessage              `json:"inputPolicy"`
	Digest        string                       `json:"digest"`
	CreatedBy     WorkflowBindingActorResponse `json:"createdBy"`
	CreatedAt     time.Time                    `json:"createdAt"`
}

type WorkflowBindingDiagnosticResponse struct {
	Severity string `json:"severity"`
	Code     string `json:"code"`
	Path     string `json:"path"`
	Message  string `json:"message"`
}

type WorkflowBindingValidationResponse struct {
	Diagnostics []WorkflowBindingDiagnosticResponse `json:"diagnostics"`
	Digest      string                              `json:"digest,omitempty"`
}

type WorkflowBindingCreateResponse struct {
	Binding  WorkflowBindingResponse         `json:"binding"`
	Revision WorkflowBindingRevisionResponse `json:"revision"`
}

type WorkflowBindingRevisionMutationResponse struct {
	Binding  WorkflowBindingResponse         `json:"binding"`
	Revision WorkflowBindingRevisionResponse `json:"revision"`
}

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

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