package server

import (
	"context"
	"encoding/json"
	"fmt"

	mcp "github.com/danieljustus/symaira-vault/internal/mcp"
	"github.com/danieljustus/symaira-vault/internal/metrics"
	vaultpkg "github.com/danieljustus/symaira-vault/internal/vault"
)

type listEntrySummary struct {
	Path       string `json:"path"`
	Type       string `json:"type,omitempty"`
	UsageHint  string `json:"usage_hint,omitempty"`
	AutoRotate bool   `json:"auto_rotate,omitempty"`
	HasValue   bool   `json:"has_value,omitempty"`
	FieldCount int    `json:"field_count,omitempty"`
}

func (s *Server) handleList(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
	prefix, err := req.RequireString("prefix")
	if err != nil {
		prefix = ""
	}

	if !s.checkScope(prefix) {
		s.logAudit(ctx, "list", prefix, false)
		metrics.RecordAuthDenial("scope_denied", s.agent.Name)
		return nil, fmt.Errorf("access denied: path %q outside allowed scope", prefix)
	}

	_, span := metrics.StartSpan(ctx, "vault.List")
	paths, err := vaultpkg.List(s.vault.Dir, prefix)
	span.End()
	if err != nil {
		s.logAudit(ctx, "list", prefix, false)
		metrics.RecordVaultOperation("list", "error")
		return vaultServiceErrorResult(err)
	}

	s.logAudit(ctx, "list", prefix, true)
	metrics.RecordVaultOperation("list", "success")

	includeDetails := req.GetBool("include_details", true)

	if !includeDetails {
		result, marshalErr := json.Marshal(paths)
		if marshalErr != nil {
			return nil, marshalErr
		}
		return mcp.NewToolResultText(string(result)), nil
	}

	summaries := make([]listEntrySummary, 0, len(paths))
	for _, path := range paths {
		entry, getErr := vaultpkg.ReadEntry(s.vault.Dir, path, s.vault.Identity)
		if getErr != nil {
			continue
		}

		summary := listEntrySummary{
			Path:       globalChokepoint.SanitizeForMCP(path),
			Type:       string(entry.SecretMetadata.Type),
			UsageHint:  globalChokepoint.SanitizeForMCP(entry.SecretMetadata.UsageHint),
			AutoRotate: entry.SecretMetadata.AutoRotate,
			HasValue:   len(entry.Data) > 0,
			FieldCount: len(entry.Data),
		}
		summaries = append(summaries, summary)
	}

	result, err := json.Marshal(summaries)
	if err != nil {
		return nil, err
	}
	return mcp.NewToolResultText(string(result)), nil
}
