package entityreconcile

import (
	"fmt"
	"io/fs"

	rootassets "gitea.maximumdirect.net/eric/notarius/assets"
	"gitea.maximumdirect.net/eric/notarius/internal/framework/llm"
)

const (
	ResponseSchemaKey  = llm.ResponseSchemaKey("dnd_entity_reconcile_llm")
	ResponseSchemaID   = "notarius.dnd.entity_reconcile.llm"
	ResponseSchemaName = "notarius_dnd_entity_reconcile_llm_v1"
	SchemaVersion      = "v1"
	SchemaAssetPath    = "schemas/dnd_entity_reconcile_llm.v1.json"
)

func schemaAssetFS() (fs.FS, error) {
	assets, err := fs.Sub(rootassets.FS(), "dnd/entity-reconciliation")
	if err != nil {
		return nil, fmt.Errorf("scope entity reconciliation assets: %w", err)
	}
	return assets, nil
}

// LoadResponseSchema returns the shared private duplicate-group response
// contract. It is intentionally separate from durable artifact schemas.
func LoadResponseSchema() (llm.ResponseSchema, error) {
	assets, err := schemaAssetFS()
	if err != nil {
		return llm.ResponseSchema{}, err
	}
	return llm.LoadResponseSchema(assets, llm.ResponseSchemaDefinition{
		Key:       ResponseSchemaKey,
		ID:        ResponseSchemaID,
		Version:   SchemaVersion,
		Name:      ResponseSchemaName,
		AssetPath: SchemaAssetPath,
	})
}

// RegisterSchemaAssets makes the shared private response schema available to
// prompt preparation. A family registrar can register it once for all consumers.
func RegisterSchemaAssets(registry *llm.AssetRegistry) error {
	assets, err := schemaAssetFS()
	if err != nil {
		return err
	}
	schemas, err := fs.Sub(assets, "schemas")
	if err != nil {
		return fmt.Errorf("scope entity reconciliation schemas: %w", err)
	}
	return registry.RegisterSchemaFS(schemas, ".")
}
