package webagent

import (
	"context"

	"github.com/tidbcloud/AgentTeam/internal/store/agent"
)

func (fake *fakeStore) ListAgentDirectoryForWorkspace(_ context.Context, _ string, _ string, options agent.AgentDirectoryOptions) (agent.AgentDirectoryPage, error) {
	fake.mu.Lock()
	defer fake.mu.Unlock()
	fake.lastDirectory = options
	if fake.directoryErr != nil {
		return agent.AgentDirectoryPage{}, fake.directoryErr
	}
	return fake.directoryPage, nil
}

func (fake *fakeStore) LookupAgentDirectoryForWorkspace(_ context.Context, _ string, _ string, agentIDs []string) ([]agent.AgentDirectoryItem, error) {
	fake.mu.Lock()
	defer fake.mu.Unlock()
	requested := make(map[string]struct{}, len(agentIDs))
	for _, agentID := range agentIDs {
		requested[agentID] = struct{}{}
	}
	items := make([]agent.AgentDirectoryItem, 0, len(agentIDs))
	for _, item := range fake.directoryPage.Agents {
		if _, ok := requested[item.ID]; ok {
			items = append(items, item)
		}
	}
	return items, fake.directoryErr
}

func (fake *fakeStore) AgentDetailForWorkspace(_ context.Context, _ string, _ string, agentID string) (agent.AppAgent, error) {
	return fake.AgentForWorkspace(context.Background(), "", "", agentID)
}

func (fake *fakeStore) AgentRuntimeFailureForWorkspace(_ context.Context, _ string, _ string, agentID string) (*agent.AgentRuntimeFailureSummary, error) {
	fake.mu.Lock()
	defer fake.mu.Unlock()
	fake.lastRuntimeFailureID = agentID
	if fake.runtimeFailureErr != nil {
		return nil, fake.runtimeFailureErr
	}
	return fake.runtimeFailure, nil
}

func (fake *fakeStore) AgentRuntimeFailuresForWorkspace(_ context.Context, _ string, _ string, agentIDs []string) (map[string]agent.AgentRuntimeFailureSummary, error) {
	fake.mu.Lock()
	defer fake.mu.Unlock()
	fake.lastRuntimeFailureIDs = append([]string(nil), agentIDs...)
	if fake.runtimeFailuresErr != nil {
		return nil, fake.runtimeFailuresErr
	}
	return fake.runtimeFailures, nil
}
