package ports

import (
	"context"
	"time"

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

// KnowledgeEntry is a knowledge-base document/investigation/runbook.
type KnowledgeEntry struct {
	Slug      string
	Title     string
	Kind      string // "document", "investigation", "runbook"
	Tags      []string
	Content   string
	About     []string // entity slugs the entry describes
	UpdatedAt time.Time
}

// KnowledgeUpsertInput is the knowledge write: row + revision + about-edges
// in one transaction.
type KnowledgeUpsertInput struct {
	Entry  KnowledgeEntry
	Audit  []AuditEntry
	Event  *Event
}

// KnowledgeRepository is the knowledge aggregate.
type KnowledgeRepository interface {
	Search(ctx context.Context, query string, limit int) ([]KnowledgeEntry, error)
	GetContent(ctx context.Context, slug string) (KnowledgeEntry, error)
	Revisions(ctx context.Context, slug string, limit int) ([]KnowledgeEntry, error)
	Tags(ctx context.Context) (map[string]int, error)
	Orphans(ctx context.Context, staleDays int) ([]KnowledgeEntry, error)
	Duplicates(ctx context.Context, threshold float64) ([]KnowledgeEntry, error)

	Upsert(ctx context.Context, input KnowledgeUpsertInput) (KnowledgeEntry, error)
	Merge(ctx context.Context, targetSlug string, sourceSlugs []string) error
	SoftDelete(ctx context.Context, slug string) error
	Restore(ctx context.Context, slug string) error
}

// LearningRepository — patterns, feedback, skills (reads plus simple
// upserts; no multi-aggregate transactions here).
type LearningRepository interface {
	ListFeedback(ctx context.Context, limit int) ([]domain.Pattern, error)
	ListPatterns(ctx context.Context, limit int) ([]domain.Pattern, error)
	ListSkills(ctx context.Context) ([]domain.Pattern, error)

	UpsertPattern(ctx context.Context, pattern domain.Pattern) error
	Validate(ctx context.Context, patternID domain.UUID) error
	Quarantine(ctx context.Context, patternID domain.UUID, reason string) error
}
