/**
 * Integration tests for Phoenix API Railing routes:
 * /api/v1/tenants/me/resources, /api/v1/tenants/me/health (tenant-scoped).
 */

import { describe, it, expect, beforeAll, afterAll } from 'vitest'
import Fastify from 'fastify'
import { registerPhoenixRailingRoutes } from '../../routes/phoenix-railing.js'

describe('Phoenix Railing routes', () => {
  let fastify: Awaited<ReturnType<typeof Fastify>>

  beforeAll(async () => {
    fastify = Fastify({ logger: false })
    fastify.decorateRequest('tenantContext', null)
    await registerPhoenixRailingRoutes(fastify)
  })

  afterAll(async () => {
    await fastify.close()
  })

  describe('GET /api/v1/tenants/me/resources', () => {
    it('returns 401 when no tenant context', async () => {
      const res = await fastify.inject({ method: 'GET', url: '/api/v1/tenants/me/resources' })
      expect(res.statusCode).toBe(401)
      const body = JSON.parse(res.payload)
      expect(body.error).toMatch(/tenant|required/i)
    })
  })

  describe('GET /api/v1/tenants/me/health', () => {
    it('returns 401 when no tenant context', async () => {
      const res = await fastify.inject({ method: 'GET', url: '/api/v1/tenants/me/health' })
      expect(res.statusCode).toBe(401)
      const body = JSON.parse(res.payload)
      expect(body.error).toBeDefined()
    })
  })
})
