package ports

import (
	"context"
	"time"

	"github.com/dtoro/oikos/internal/core/domain"
)

// Event is a domain occurrence worth publishing (SSE + events table). The
// events adapter maps it to the persistence/SSE shapes.
type Event struct {
	Type     string // e.g. "execution.completed", "health.changed"
	Severity string // "info", "warning", "critical"
	Source   string // "api", "mcp", "scheduler", "webhook"
	EntityID      domain.UUID
	CorrelationID string
	Data          map[string]any
	Ts            time.Time
}

// EventPublisher fans events out to subscribers and persists them. The
// events adapter owns the SSE broker, the events table, and the dedicated
// LISTEN/NOTIFY connection.
type EventPublisher interface {
	Publish(ctx context.Context, event Event) error
}

// AuditEntry is an append-only audit-log record. Method/Path carry the
// surface context ("POST", "/api/v1/entities" for REST; "TOOL",
// "create_entity" for MCP).
type AuditEntry struct {
	ActorType  string // "agent", "operator", "system"
	ActorLabel string
	Action     string
	EntityID   domain.UUID
	Method     string
	Path       string
	CorrelationID string
	Details    map[string]any
	Ts         time.Time
}

// AuditRepository appends audit records and events. Entries are usually
// carried inside other repositories' input structs so they commit in the
// same transaction (§3.6 of the plan); the standalone methods serve
// read-path actions that audit without another aggregate write.
type AuditRepository interface {
	AppendAudit(ctx context.Context, entries []AuditEntry) error
	AppendEvent(ctx context.Context, events []Event) error
}
