// Package teamworkflow owns the creator-private browser transport for Team
// Workflow relations. Authoring, direct publication, and deletion orchestration
// remain behind Service and are intentionally absent from these handlers.
package teamworkflow

import (
	"context"
	"time"

	"github.com/tidbcloud/AgentTeam/internal/workflow/planmanage"
)

type Failure struct {
	Code    string
	Message string
}

type Actions struct {
	CanOpen    bool
	CanRetry   bool
	CanArchive bool
	CanRestore bool
	CanDelete  bool
}

// Item is the API-facing domain projection. ID is the Team Workflow relation
// id. Plan remains optional because generating and pre-bind failures must still
// be readable and deletable.
type Item struct {
	ID              string
	TeamID          string
	Revision        int64
	Status          string
	AuthoringStatus string
	Availability    string
	Plan            *planmanage.Plan
	Failure         *Failure
	Actions         Actions
	CreatedAt       time.Time
	UpdatedAt       time.Time
}

type Page struct {
	Items      []Item
	NextCursor string
}

type ListInput struct {
	UserID      string
	WorkspaceID string
	TeamID      string
	Search      string
	State       string
	Limit       int
	Cursor      string
}

type GetInput struct {
	UserID         string
	WorkspaceID    string
	TeamID         string
	TeamWorkflowID string
}

type DeleteInput struct {
	UserID           string
	WorkspaceID      string
	TeamID           string
	TeamWorkflowID   string
	ExpectedRevision int64
}

type LifecycleInput struct {
	UserID           string
	WorkspaceID      string
	TeamID           string
	TeamWorkflowID   string
	ExpectedRevision int64
	Archived         bool
}

// Service makes creator scoping explicit in every method contract. Concrete
// adapters must both authorize the Team and constrain rows to the authenticated
// creator; handler-returned action flags never replace those checks.
type Service interface {
	ListForUser(context.Context, ListInput) (Page, error)
	GetForUser(context.Context, GetInput) (Item, error)
	SetAvailabilityForUser(context.Context, LifecycleInput) (Item, error)
	DeleteForUser(context.Context, DeleteInput) error
}
