import type { ToolConfig } from '@/tools/types'
import type { SpotifySetVolumeParams, SpotifySetVolumeResponse } from './types'

export const spotifySetVolumeTool: ToolConfig<SpotifySetVolumeParams, SpotifySetVolumeResponse> = {
  id: 'spotify_set_volume',
  name: 'Spotify Set Volume',
  description: 'Set the playback volume on Spotify.',
  version: '1.0.0',

  oauth: {
    required: true,
    provider: 'spotify',
    requiredScopes: ['user-modify-playback-state'],
  },

  params: {
    volume_percent: {
      type: 'number',
      required: true,
      visibility: 'user-or-llm',
      description: 'Volume level (0 to 100)',
    },
    device_id: {
      type: 'string',
      required: false,
      visibility: 'user-or-llm',
      description: 'Device ID. If not provided, uses active device.',
    },
  },

  request: {
    url: (params) => {
      const volume = Math.min(Math.max(params.volume_percent, 0), 100)
      let url = `https://api.spotify.com/v1/me/player/volume?volume_percent=${volume}`
      if (params.device_id) {
        url += `&device_id=${params.device_id}`
      }
      return url
    },
    method: 'PUT',
    headers: (params) => ({
      Authorization: `Bearer ${params.accessToken}`,
      'Content-Type': 'application/json',
    }),
  },

  transformResponse: async (): Promise<SpotifySetVolumeResponse> => {
    return {
      success: true,
      output: {
        success: true,
      },
    }
  },

  outputs: {
    success: { type: 'boolean', description: 'Whether volume was set' },
  },
}
