// Package projectmemory stores durable, typed project knowledge (decisions,
// conventions, file maps, gotchas) with optional embeddings. Write optionally
// embeds title+body (best-effort; null embedding when no embedder is configured).
// Search is hybrid: vector KNN when embeddings exist, ILIKE/tags fallback else.
// RenderAgentsMD groups entries into a deterministic AGENTS.md seeded into the
// worktree before an agent spawns.
package projectmemory

import (
	"time"

	"github.com/google/uuid"
)

// Kind values mirror the project_memory.kind CHECK constraint.
const (
	KindDecision   = "decision"
	KindConvention = "convention"
	KindFileMap    = "file_map"
	KindGotcha     = "gotcha"
)

// validKinds is the ordered set used by RenderAgentsMD for deterministic output.
var validKinds = []string{KindDecision, KindConvention, KindFileMap, KindGotcha}

// IsValidKind reports whether k is an allowed memory kind.
func IsValidKind(k string) bool {
	for _, v := range validKinds {
		if v == k {
			return true
		}
	}
	return false
}

// Source values mirror the project_memory.source CHECK constraint.
const (
	SourceAgent = "agent"
	SourceUser  = "user"
	SourceAuto  = "auto"
)

// Entry mirrors a project_memory row (embedding excluded from the domain type).
type Entry struct {
	ID             uuid.UUID
	ProjectID      uuid.UUID
	WorkspaceID    *uuid.UUID
	Kind           string
	Title          string
	Body           string
	Tags           []string
	Source         string
	EmbeddingModel *string
	CreatedBy      *uuid.UUID
	CreatedAt      time.Time
	UpdatedAt      time.Time
}

// WriteInput is the projectmemory.Write payload.
type WriteInput struct {
	ProjectID   uuid.UUID
	WorkspaceID *uuid.UUID
	Kind        string
	Title       string
	Body        string
	Tags        []string
	Source      string
	CreatedBy   *uuid.UUID
}

// Query parameterizes a memory search.
type Query struct {
	ProjectID uuid.UUID
	Text      string
	Kind      string
	TopK      int
}
