// Package run owns the authenticated human HTTP transport for
// first-class Workflow Run observation and cancellation.
package run

import (
	"context"

	runstore "github.com/tidbcloud/AgentTeam/internal/store/workflow/run"
	"github.com/tidbcloud/AgentTeam/internal/workflow/calendar"
	"github.com/tidbcloud/AgentTeam/internal/workflow/runmanage"
)

type Actor = runmanage.Actor

const (
	ActorTypeHuman = runmanage.ActorTypeHuman
	ActorTypeAgent = runmanage.ActorTypeAgent
)

type ListRunsInput = runmanage.ListRunsInput
type ListChildrenInput = runmanage.ListChildrenInput
type ListEventsInput = runmanage.ListEventsInput
type CancelInput = runmanage.CancelInput
type RunPage = runmanage.RunPage
type ActivationPage = runmanage.ActivationPage
type EffectPage = runmanage.EffectPage
type EventPage = runmanage.EventPage
type ArtifactReadInput = runmanage.ArtifactReadInput
type ArtifactPage = runmanage.ArtifactPage
type CancelResult = runmanage.CancelResult
type AgentPrincipal = runmanage.AgentPrincipal

type Service interface {
	ListRuns(context.Context, runmanage.ListRunsInput) (runmanage.RunPage, error)
	GetRun(context.Context, string, string) (runstore.Run, error)
	ListActivations(context.Context, runmanage.ListChildrenInput) (runmanage.ActivationPage, error)
	ListEffects(context.Context, runmanage.ListChildrenInput) (runmanage.EffectPage, error)
	ListEffectQueueContexts(context.Context, string, string) (map[string]runstore.EffectQueueContext, error)
	ListEvents(context.Context, runmanage.ListEventsInput) (runmanage.EventPage, error)
	GetEvent(context.Context, string, string, int64) (runstore.Event, error)
	Cancel(context.Context, runmanage.CancelInput) (runmanage.CancelResult, error)
}

// CalendarService is kept separate from the Run lifecycle service because the
// calendar is a cross-aggregate read model, not a Run mutation capability.
// ArtifactReader is optional so existing Run services and test doubles do not
// gain an oversized-payload capability unless they explicitly implement it.
type ArtifactReader interface {
	ReadArtifactPage(context.Context, runmanage.ArtifactReadInput) (runmanage.ArtifactPage, error)
}

// AgentRunReader is the authenticated Agent transport capability. It is kept
// separate from Service so human Workflow Run handlers cannot bypass or
// accidentally inherit the Agent machine-owner policy.
type AgentRunReader interface {
	ListRunsForAgent(context.Context, runmanage.ListRunsInput, runmanage.AgentPrincipal) (runmanage.RunPage, error)
	GetRunForAgent(context.Context, string, string, runmanage.AgentPrincipal) (runstore.Run, error)
	ReadArtifactPageForAgent(context.Context, runmanage.ArtifactReadInput, runmanage.AgentPrincipal) (runmanage.ArtifactPage, error)
	ListActivationsForAgent(context.Context, runmanage.ListChildrenInput, runmanage.AgentPrincipal) (runmanage.ActivationPage, error)
	ListEffectsForAgent(context.Context, runmanage.ListChildrenInput, runmanage.AgentPrincipal) (runmanage.EffectPage, error)
	ListEventsForAgent(context.Context, runmanage.ListEventsInput, runmanage.AgentPrincipal) (runmanage.EventPage, error)
	GetEventForAgent(context.Context, string, string, int64, runmanage.AgentPrincipal) (runstore.Event, error)
	CancelForAgent(context.Context, runmanage.CancelInput, runmanage.AgentPrincipal) (runmanage.CancelResult, error)
}

type CalendarService interface {
	List(context.Context, calendar.ListInput) (calendar.Result, error)
}
