package port

import (
	"context"

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

// WebhookRepository defines operations for webhook storage.
type WebhookRepository interface {
	// Create creates a new webhook subscription.
	Create(ctx context.Context, webhook *domain.Webhook) error

	// Update updates an existing webhook.
	Update(ctx context.Context, webhook *domain.Webhook) error

	// Delete deletes a webhook by ID.
	Delete(ctx context.Context, id domain.WebhookID) error

	// GetByID retrieves a webhook by ID.
	GetByID(ctx context.Context, id domain.WebhookID) (*domain.Webhook, error)

	// ListByProject returns all webhooks for a project.
	ListByProject(ctx context.Context, projectID string) ([]*domain.Webhook, error)

	// ListEnabledByProjectAndEvent returns enabled webhooks that subscribe to a specific event type.
	ListEnabledByProjectAndEvent(ctx context.Context, projectID string, eventType domain.WebhookEventType) ([]*domain.Webhook, error)

	// RecordDelivery records a webhook delivery attempt.
	RecordDelivery(ctx context.Context, delivery *domain.WebhookDelivery) error

	// GetDeliveries returns delivery history for a webhook.
	GetDeliveries(ctx context.Context, webhookID domain.WebhookID, filters *domain.WebhookDeliveryFilters) ([]*domain.WebhookDelivery, error)

	// CleanupOldDeliveries removes delivery records older than the specified number of days.
	CleanupOldDeliveries(ctx context.Context, olderThanDays int) (int64, error)
}

// WebhookDispatcher defines operations for dispatching webhook events.
type WebhookDispatcher interface {
	// Dispatch sends an event to all subscribed webhooks for a project.
	// This is a non-blocking operation - deliveries happen in the background.
	Dispatch(ctx context.Context, projectID string, event *domain.WebhookEvent) error

	// Start starts the background dispatcher workers.
	Start() error

	// Stop gracefully shuts down the dispatcher.
	Stop()
}
