import { z } from '@frontmcp/lazy-zod';
import { Tool, ToolContext } from '@frontmcp/sdk';

const inputSchema = {
  key: z.string().describe('Environment variable key to retrieve'),
  defaultValue: z.string().optional().describe('Default value if key is not found'),
};

const outputSchema = z.object({
  key: z.string(),
  value: z.string().nullable(),
  found: z.boolean(),
  defaultUsed: z.boolean(),
});

type Input = z.infer<z.ZodObject<typeof inputSchema>>;
type Output = z.infer<typeof outputSchema>;

@Tool({
  name: 'get-config',
  description: 'Get an environment variable value with optional default',
  inputSchema,
  outputSchema,
})
export default class GetConfigTool extends ToolContext {
  async execute(input: Input): Promise<Output> {
    const found = this.config.has(input.key);
    // Use default value only if provided, otherwise get raw value
    const value =
      input.defaultValue !== undefined ? this.config.get(input.key, input.defaultValue) : this.config.get(input.key);

    return {
      key: input.key,
      value: value ?? null,
      found,
      defaultUsed: !found && input.defaultValue !== undefined,
    };
  }
}
