-- The embedding (vector) column is excluded from sqlc queries (no native pgvector
-- type). Write sets embedding via a hand-written pgx UPDATE; vector KNN search is
-- hand-written in repository.go. These queries cover the non-vector path.

-- name: InsertEntry :one
INSERT INTO project_memory (
  id, project_id, workspace_id, kind, title, body, tags, source,
  embedding_model, created_by
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
RETURNING id, project_id, workspace_id, kind, title, body, tags, source,
          embedding_model, created_by, created_at, updated_at;

-- name: GetEntry :one
SELECT id, project_id, workspace_id, kind, title, body, tags, source,
       embedding_model, created_by, created_at, updated_at
FROM project_memory
WHERE id = $1;

-- name: ListByProject :many
SELECT id, project_id, workspace_id, kind, title, body, tags, source,
       embedding_model, created_by, created_at, updated_at
FROM project_memory
WHERE project_id = $1
  AND ($2::text = '' OR kind = $2)
ORDER BY kind ASC, created_at DESC;

-- name: SearchByKeyword :many
SELECT id, project_id, workspace_id, kind, title, body, tags, source,
       embedding_model, created_by, created_at, updated_at
FROM project_memory
WHERE project_id = $1
  AND ($2::text = '' OR kind = $2)
  AND ($3::text = '' OR title ILIKE '%' || $3 || '%' OR body ILIKE '%' || $3 || '%' OR $3 = ANY(tags))
ORDER BY updated_at DESC
LIMIT $4;

-- name: DeleteEntry :exec
DELETE FROM project_memory WHERE id = $1;
