package dto

import (
	"encoding/json"
	"time"
)

// WorkflowInvocationCreateRequest accepts only caller-authored Plan input.
// PlanVersionID is optional; omission selects the Plan's default immutable
// version. Identity and provenance fields come only from trusted server state.
// TeamID optionally names the Team for an unlinked/dynamic Run; the server
// authorizes the caller's Team membership before freezing it. A Team-linked
// Plan keeps its authoritative relation Team.
type WorkflowInvocationCreateRequest struct {
	PlanVersionID string          `json:"planVersionId,omitempty"`
	TeamID        string          `json:"teamId,omitempty"`
	Input         json.RawMessage `json:"input"`
}

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

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

type WorkflowInvocationSourceResponse struct {
	Kind          string          `json:"kind"`
	OccurrenceRef string          `json:"occurrenceRef"`
	OccurredAt    time.Time       `json:"occurredAt"`
	Provenance    json.RawMessage `json:"provenance,omitempty"`
}

type WorkflowInvocationExecutionOwnerResponse struct {
	UserID   string `json:"userId"`
	MemberID string `json:"memberId"`
}

type WorkflowInvocationActorResponse struct {
	Type     string `json:"type"`
	ID       string `json:"id"`
	MemberID string `json:"memberId"`
}

// WorkflowInvocationResponse is the immutable accepted occurrence snapshot.
// RunID is output-only and identifies the one Run atomically created with it.
type WorkflowInvocationResponse struct {
	ID                string                                   `json:"id"`
	BindingID         string                                   `json:"bindingId,omitempty"`
	BindingRevision   *WorkflowInvocationRevisionRefResponse   `json:"bindingRevision,omitempty"`
	BindingSourceID   string                                   `json:"bindingSourceId,omitempty"`
	BindingActivation int64                                    `json:"bindingActivation,omitempty"`
	PlanID            string                                   `json:"planId"`
	PlanVersion       WorkflowInvocationVersionRefResponse     `json:"planVersion"`
	Source            WorkflowInvocationSourceResponse         `json:"source"`
	Input             json.RawMessage                          `json:"input,omitempty"`
	InputDigest       string                                   `json:"inputDigest"`
	ExecutionOwner    WorkflowInvocationExecutionOwnerResponse `json:"executionOwner"`
	PrincipalMemberID string                                   `json:"principalMemberId"`
	AuthorMemberID    string                                   `json:"authorMemberId"`
	InvokedBy         *WorkflowInvocationActorResponse         `json:"invokedBy,omitempty"`
	RunID             string                                   `json:"runId"`
	RunStatus         string                                   `json:"runStatus"`
	AcceptedAt        time.Time                                `json:"acceptedAt"`
	Replayed          bool                                     `json:"replayed,omitempty"`
}

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