package store

import (
	"context"

	"chat-switchboard/models"
)

// WorkflowStore manages workflow definitions, stages, and version snapshots.
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)

	// ── Assignments (v0.29.0-cs3) ──

	// CreateAssignment inserts a workflow_assignments row.
	CreateAssignment(ctx context.Context, a *WorkflowAssignment) error

	// ListAssignmentsForTeam returns assignments for a team filtered by status.
	ListAssignmentsForTeam(ctx context.Context, teamID, status string) ([]WorkflowAssignment, error)

	// ListAssignmentsMine returns claimed + unassigned assignments visible to a user.
	ListAssignmentsMine(ctx context.Context, userID string) ([]WorkflowAssignment, error)

	// ClaimAssignment sets assigned_to and status='claimed' on an unassigned row.
	// Returns rows affected (0 = already claimed or not found).
	ClaimAssignment(ctx context.Context, assignmentID, userID string) (int64, error)

	// CompleteAssignment sets status='completed' on a claimed row.
	// Returns rows affected (0 = not claimed or not found).
	CompleteAssignment(ctx context.Context, assignmentID string) (int64, error)

	// GetAssignmentChannelID returns the channel_id for an assignment.
	GetAssignmentChannelID(ctx context.Context, assignmentID string) (string, error)

	// TryRoundRobin finds the least-recently-assigned team member and claims.
	// Returns the assigned user ID, or "" if no members available.
	TryRoundRobin(ctx context.Context, teamID, assignmentID string) (string, error)

	// ── Lifecycle operations (v0.37.15) ──

	// CancelAssignmentsForChannel sets status='cancelled' on all
	// unassigned/claimed assignments for a channel.
	CancelAssignmentsForChannel(ctx context.Context, channelID string) (int64, error)

	// UnclaimAssignment returns a claimed assignment to unassigned.
	// Returns rows affected (0 = not claimed or not found).
	UnclaimAssignment(ctx context.Context, assignmentID string) (int64, error)

	// ReassignAssignment changes assigned_to on a claimed assignment.
	// Returns rows affected.
	ReassignAssignment(ctx context.Context, assignmentID, newUserID string) (int64, error)

	// CancelAssignment sets a single assignment to cancelled.
	CancelAssignment(ctx context.Context, assignmentID string) (int64, error)

	// ── Review Comments (v0.35.0) ──

	// GetAssignmentByID returns a single assignment by ID.
	GetAssignmentByID(ctx context.Context, id string) (*WorkflowAssignment, error)

	// AddReviewComment appends a comment to an assignment's review_comments array.
	AddReviewComment(ctx context.Context, assignmentID string, comment ReviewComment) error
}

// ReviewComment is a single review comment on a workflow assignment (v0.35.0).
type ReviewComment struct {
	Text      string `json:"text"`
	UserID    string `json:"user_id"`
	CreatedAt string `json:"created_at"`
}
