import { describe, it, expect, vi } from 'vitest'
import { GET } from './route'
import { NextRequest } from 'next/server'

vi.mock('@/lib/prisma', () => ({
  prisma: {
    weatherCache: {
      findUnique: vi.fn().mockResolvedValue(null),
      upsert: vi.fn().mockResolvedValue({}),
    },
  },
}))

vi.mock('@/lib/weather', () => ({
  fetchOpenMeteo: vi.fn().mockResolvedValue({
    temperature: 22, windspeed: 15, winddirection: 270, weathercode: 1, time: '2025-01-01T12:00', source: 'openmeteo',
  }),
  wmoCodeToLabel: vi.fn(),
  isGoodForFlying: vi.fn(),
}))

describe('GET /api/weather', () => {
  it('returns 400 without lat/lng', async () => {
    const res = await GET(new NextRequest('http://localhost/api/weather'))
    expect(res.status).toBe(400)
  })
  it('returns 200 with valid lat/lng', async () => {
    const res = await GET(new NextRequest('http://localhost/api/weather?lat=38.71&lng=-9.13'))
    expect(res.status).toBe(200)
    const data = await res.json()
    expect(data).toHaveProperty('temperature')
  })
})
