import type { RedisLPushParams, RedisLPushResponse } from '@/tools/redis/types'
import type { ToolConfig } from '@/tools/types'

export const redisLPushTool: ToolConfig<RedisLPushParams, RedisLPushResponse> = {
  id: 'redis_lpush',
  name: 'Redis LPUSH',
  description: 'Prepend a value to a list stored at a key in Redis.',
  version: '1.0.0',

  params: {
    url: {
      type: 'string',
      required: true,
      visibility: 'user-only',
      description: 'Redis connection URL (e.g. redis://user:password@host:port)',
    },
    key: {
      type: 'string',
      required: true,
      visibility: 'user-or-llm',
      description: 'The list key',
    },
    value: {
      type: 'string',
      required: true,
      visibility: 'user-or-llm',
      description: 'The value to prepend',
    },
  },

  request: {
    url: '/api/tools/redis/execute',
    method: 'POST',
    headers: () => ({ 'Content-Type': 'application/json' }),
    body: (params) => ({
      url: params.url,
      command: 'LPUSH',
      args: [params.key, params.value],
    }),
  },

  transformResponse: async (response: Response, params?: RedisLPushParams) => {
    const data = await response.json()

    if (!response.ok) {
      throw new Error(data.error || 'Failed to push to list in Redis')
    }

    return {
      success: true,
      output: {
        key: params?.key ?? '',
        length: data.result ?? 0,
      },
    }
  },

  outputs: {
    key: { type: 'string', description: 'The list key' },
    length: { type: 'number', description: 'Length of the list after the push' },
  },
}
