import type { IntegrationAdapter } from './types.js'

export const weather: IntegrationAdapter = {
  async testConnection(config) {
    const location = config.location
    if (!location) return { ok: false, message: 'Missing location' }
    try {
      const res = await fetch(`https://wttr.in/${encodeURIComponent(location)}?format=j1`, {
        headers: { 'User-Agent': 'curl' },
      })
      if (!res.ok) return { ok: false, message: `HTTP ${res.status}` }
      const body = (await res.json()) as { current_condition?: unknown[] }
      if (!body.current_condition?.length) return { ok: false, message: 'Location not found' }
      return { ok: true, message: 'Connected' }
    } catch (err) {
      return { ok: false, message: err instanceof Error ? err.message : 'Connection failed' }
    }
  },
}
