package port

import (
	"context"

	"github.com/orchard9/rdev/internal/domain"
)

// CommandQueue defines operations for the command queue repository.
type CommandQueue interface {
	// Enqueue adds a command to the queue.
	Enqueue(ctx context.Context, cmd *domain.QueuedCommand) error

	// Dequeue retrieves and locks the next pending command for a project.
	// Returns nil if no commands are pending.
	// The command status is atomically updated to 'running'.
	Dequeue(ctx context.Context, projectID string) (*domain.QueuedCommand, error)

	// UpdateStatus updates the status of a queued command.
	// If result is provided, it also updates the result fields.
	UpdateStatus(ctx context.Context, cmdID domain.QueuedCommandID, status domain.QueueStatus, result *domain.QueuedCommandResult) error

	// GetByID retrieves a specific queued command by ID.
	GetByID(ctx context.Context, cmdID domain.QueuedCommandID) (*domain.QueuedCommand, error)

	// List returns queued commands for a project with optional filters.
	List(ctx context.Context, projectID string, filters *domain.QueueFilters) ([]*domain.QueuedCommand, error)

	// Cancel marks a pending command as cancelled.
	// Returns an error if the command is not in pending status.
	Cancel(ctx context.Context, cmdID domain.QueuedCommandID) error

	// GetStats returns queue statistics for a project (or all projects if empty).
	GetStats(ctx context.Context, projectID string) (*domain.QueueStats, error)

	// CleanupOld removes completed/failed/cancelled commands older than the specified duration.
	CleanupOld(ctx context.Context, olderThanDays int) (int64, error)
}
