package store

import (
	"context"
	"encoding/json"

	"switchboard-core/models"
)

// WorkflowStore manages workflow definitions, stages, version snapshots,
// instances, and assignments.
type WorkflowStore interface {
	// Workflow CRUD
	Create(ctx context.Context, w *models.Workflow) error
	GetByID(ctx context.Context, id string) (*models.Workflow, error)
	GetBySlug(ctx context.Context, teamID *string, slug string) (*models.Workflow, error)
	Update(ctx context.Context, id string, patch models.WorkflowPatch) error
	Delete(ctx context.Context, id string) error
	ListForTeam(ctx context.Context, teamID string) ([]models.Workflow, error)
	ListGlobal(ctx context.Context) ([]models.Workflow, error)

	// Stage CRUD
	CreateStage(ctx context.Context, s *models.WorkflowStage) error
	ListStages(ctx context.Context, workflowID string) ([]models.WorkflowStage, error)
	UpdateStage(ctx context.Context, s *models.WorkflowStage) error
	DeleteStage(ctx context.Context, id string) error
	ReorderStages(ctx context.Context, workflowID string, orderedIDs []string) error

	// Versioning
	Publish(ctx context.Context, v *models.WorkflowVersion) error
	GetVersion(ctx context.Context, workflowID string, versionNumber int) (*models.WorkflowVersion, error)
	GetLatestVersion(ctx context.Context, workflowID string) (*models.WorkflowVersion, error)

	// Instances (v0.3.1+)
	CreateInstance(ctx context.Context, inst *models.WorkflowInstance) error
	GetInstance(ctx context.Context, id string) (*models.WorkflowInstance, error)
	GetInstanceByToken(ctx context.Context, token string) (*models.WorkflowInstance, error)
	UpdateInstance(ctx context.Context, inst *models.WorkflowInstance) error
	ListInstances(ctx context.Context, workflowID string, status string, opts ListOptions) ([]models.WorkflowInstance, error)
	AdvanceStage(ctx context.Context, id string, nextStage string, stageData json.RawMessage) error
	CompleteInstance(ctx context.Context, id string) error
	CancelInstance(ctx context.Context, id string) error
	MarkInstanceStale(ctx context.Context, id string) error
	ListActiveInstances(ctx context.Context) ([]models.WorkflowInstance, error)

	// Assignments (v0.3.1)
	CreateAssignment(ctx context.Context, a *models.WorkflowAssignment) error
	ClaimAssignment(ctx context.Context, id string, userID string) error
	UnclaimAssignment(ctx context.Context, id string) error
	CompleteAssignment(ctx context.Context, id string, reviewData json.RawMessage) error
	CancelAssignment(ctx context.Context, id string) error
	ListAssignmentsByTeam(ctx context.Context, teamID string, status string) ([]models.WorkflowAssignment, error)
	ListAssignmentsByInstance(ctx context.Context, instanceID string) ([]models.WorkflowAssignment, error)
	ListAssignmentsByUser(ctx context.Context, userID string, status string) ([]models.WorkflowAssignment, error)

	// Signoffs (v0.3.4)
	CreateSignoff(ctx context.Context, s *models.WorkflowSignoff) error
	ListSignoffs(ctx context.Context, instanceID, stage string) ([]models.WorkflowSignoff, error)
	CountSignoffs(ctx context.Context, instanceID, stage, decision string) (int, error)
}
