package httpapi

import (
	"context"

	"github.com/dtoro/oikos/internal/checkdefaults"
	"github.com/dtoro/oikos/internal/db"
	"github.com/google/uuid"
	"github.com/jackc/pgx/v5"
)

// ensureDefaultChecks derives an entity's default checks from the monitoring
// kinds its type declares.
//
// Note the ordering caveat: an entity created through the API usually has no
// edges yet, so a type whose address comes from its host (a service) will
// produce no checks on this pass. That gap is real and deliberately visible —
// coverageSweep reports it, and the next inventory ingest fills it in once
// the hosting edge exists.
func ensureDefaultChecks(ctx context.Context, tx pgx.Tx, entityID uuid.UUID, slug, entityType, name string, attrsJSON []byte) error {
	tree, err := db.LoadTypeTree(ctx, tx)
	if err != nil {
		return err
	}
	res, err := checkdefaults.Ensure(ctx, tx, tree, checkdefaults.Target{
		ID: entityID, Slug: slug, Type: entityType, Name: name, Attrs: attrsJSON,
	})
	if err != nil {
		return err
	}
	checkdefaults.LogResult(slug, entityType, res)
	return nil
}
