import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import request from 'supertest';
import { AppModule } from '../src/app.module';

describe('AppController (e2e)', () => {
  let app: INestApplication;

  beforeAll(async () => {
    const moduleFixture: TestingModule = await Test.createTestingModule({
      imports: [AppModule],
    }).compile();
    app = moduleFixture.createNestApplication();
    app.setGlobalPrefix('api');
    await app.init();
  });

  afterAll(async () => {
    await app?.close();
  });

  it('GET /api → 200 with standard response', async () => {
    const res = await request(app.getHttpServer())
      .get('/api')
      .expect(200);
    expect(res.body).toHaveProperty('success', true);
    expect(res.body).toHaveProperty('data');
    expect(res.body).toHaveProperty('timestamp');
  });

  it('GET /api → returns x-trace-id header', async () => {
    const res = await request(app.getHttpServer()).get('/api');
    expect(res.headers).toHaveProperty('x-trace-id');
  });

  it('POST /api/not-found → 404', async () => {
    const res = await request(app.getHttpServer())
      .post('/api/not-found')
      .expect(404);
    expect(res.body.success).toBe(false);
  });
});
