import { test } from '@japa/runner'

test.group('Images show', () => {
  test('get uploaded image', async ({ client, assert }) => {
    // 1. Login to get token
    const loginResponse = await client.post('/api/login').json({
      email: 'kiss.david@test.hu',
      password: 'Password123',
    })

    loginResponse.assertStatus(200)
    const token = loginResponse.body().token

    // 2. Upload an image
    const response = await client
      .post('/api/images/upload')
      .header('Authorization', `Bearer ${token}`)
      .file('image', Buffer.from('fake image content'), {
        filename: 'test.jpg',
        contentType: 'image/jpeg',
      })

    response.assertStatus(200)
    const { url } = response.body()

    // 3. Try to fetch the image
    const showResponse = await client.get(url)

    if (showResponse.status() === 404) {
      console.log('404 Body:', showResponse.body())
    }

    showResponse.assertStatus(200)
    assert.equal(showResponse.header('content-type'), 'image/jpeg')
  })

  test('get uploaded thumbnail', async ({ client, assert }) => {
    // 1. Login to get token
    const loginResponse = await client.post('/api/login').json({
      email: 'kiss.david@test.hu',
      password: 'Password123',
    })

    loginResponse.assertStatus(200)
    const token = loginResponse.body().token

    const response = await client
      .post('/api/images/upload')
      .header('Authorization', `Bearer ${token}`)
      .file('image', Buffer.from('fake image content'), {
        filename: 'test.jpg',
        contentType: 'image/jpeg',
      })

    response.assertStatus(200)
    const { thumbnailUrl } = response.body()

    const showResponse = await client.get(thumbnailUrl)
    showResponse.assertStatus(200)
    assert.equal(showResponse.header('content-type'), 'image/jpeg')
  })
})
