import type { ToolProfile, ToolOverrideConfig } from '../config/schema.js';
import type { ModelTier } from '../models/router.js';

export interface AgentConfig {
  name: string;
  systemPrompt?: string;
  modelTier?: ModelTier;
  toolProfile?: ToolProfile;
  toolOverrides?: ToolOverrideConfig;
  sandbox?: boolean;
}

/**
 * AgentConfigRegistry — stores named agent configurations.
 * Loaded from YAML config at startup.
 */
export class AgentConfigRegistry {
  private configs = new Map<string, AgentConfig>();

  register(config: AgentConfig): void {
    if (this.configs.has(config.name)) {
      throw new Error(`Agent config '${config.name}' is already registered`);
    }
    this.configs.set(config.name, config);
  }

  get(name: string): AgentConfig | undefined {
    return this.configs.get(name);
  }

  list(): AgentConfig[] {
    return Array.from(this.configs.values());
  }

  /**
   * Load agent configs from the parsed YAML config.
   * Maps from the config schema format to the internal AgentConfig format.
   */
  loadFromConfig(rawConfigs: Record<string, {
    system_prompt?: string;
    model_tier?: string;
    tool_profile?: string;
    tool_overrides?: ToolOverrideConfig;
    sandbox?: boolean;
  }>): void {
    for (const [name, raw] of Object.entries(rawConfigs)) {
      this.register({
        name,
        systemPrompt: raw.system_prompt,
        modelTier: raw.model_tier as ModelTier | undefined,
        toolProfile: raw.tool_profile as ToolProfile | undefined,
        toolOverrides: raw.tool_overrides,
        sandbox: raw.sandbox,
      });
    }
  }
}
