// Package teamworkflow owns the business lifecycle that links reusable
// Workflow Plans to one Team and publishes exact compiled draft snapshots
// under the current Team Leader.
package teamworkflow

import (
	"time"

	storeteamworkflow "github.com/tidbcloud/AgentTeam/internal/store/teamworkflow"
	"github.com/tidbcloud/AgentTeam/internal/store/workflow/plan"
)

// CreateInput creates one reusable Team Workflow and its mutable Workflow Plan
// draft atomically. SourceMessageID is a trusted daemon command id; callers do
// not supply a separate user-facing task or channel identity.
type CreateInput struct {
	TeamID          string
	SourceMessageID string
	PlanKey         string
	DisplayName     string
	DescriptionMD   string
	Source          string
	Layout          []byte
	IdempotencyKey  string
	Now             time.Time
}

type CreateResult struct {
	TeamWorkflow storeteamworkflow.TeamWorkflow
	Plan         plan.Plan
	Draft        plan.PlanDraft
	Replayed     bool
}

// UpdateInput applies a Team Workflow Plan draft directly. The Team leader is
// re-authorized inside the transaction; compilation, immutable version append,
// current/default pointer update, draft update, and relation revision advance
// commit atomically.
type UpdateInput struct {
	TeamID               string
	PlanID               string
	ExpectedPlanRevision int64
	DraftRevision        int64
	Source               string
	Layout               []byte
	MakeDefault          bool
	ClientRequestID      string
	Now                  time.Time
}

type UpdateResult struct {
	TeamWorkflow storeteamworkflow.TeamWorkflow
	Plan         plan.Plan
	Draft        plan.PlanDraft
	Version      plan.PlanVersion
	Replayed     bool
}

type AvailablePage struct {
	Items      []plan.Plan
	NextCursor string
}

// PlanInspection is the bounded Team lifecycle context needed by the Agent
// business-facing Plan get action.
type PlanInspection struct {
	TeamWorkflowID string
	TeamID         string
	Revision       int64
	AuthoringState string
}

type LifecycleInput struct {
	WorkspaceID      string
	TeamID           string
	TeamWorkflowID   string
	ActorMemberID    string
	ExpectedRevision int64
	// ExpectedPlanRevision is optional for Human management calls and required
	// by the Agent business surface to preserve its Plan-level CAS token.
	ExpectedPlanRevision int64
	AgentActor           *plan.Actor
	Archived             bool
	Now                  time.Time
}

type LifecycleResult struct {
	TeamWorkflow storeteamworkflow.TeamWorkflow
	Plan         plan.Plan
	Replayed     bool
}

type DeleteInput struct {
	WorkspaceID      string
	TeamID           string
	TeamWorkflowID   string
	ActorMemberID    string
	ExpectedRevision int64
	ClientRequestID  string
	Now              time.Time
}

type DeleteResult struct {
	TeamWorkflow storeteamworkflow.TeamWorkflow
	Replayed     bool
}
