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

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

  beforeAll(async () => {
    const moduleFixture: TestingModule = await Test.createTestingModule({
      imports: [AppModule],
    }).compile();

    app = moduleFixture.createNestApplication();
    app.setGlobalPrefix('api/v1');
    app.useGlobalPipes(new ValidationPipe({ whitelist: true, transform: true }));
    await app.init();
  });

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

  describe('Auth', () => {
    it('POST /api/v1/auth/register should validate input', () => {
      return request(app.getHttpServer())
        .post('/api/v1/auth/register')
        .send({})
        .expect(400);
    });
  });

  describe('Courses', () => {
    it('GET /api/v1/courses should return paginated results', () => {
      return request(app.getHttpServer())
        .get('/api/v1/courses')
        .expect(200)
        .expect((res) => {
          expect(res.body).toHaveProperty('items');
          expect(res.body).toHaveProperty('total');
          expect(res.body).toHaveProperty('page');
          expect(res.body).toHaveProperty('pageSize');
        });
    });

    it('GET /api/v1/courses/:id should return 404 for non-existent', () => {
      return request(app.getHttpServer())
        .get('/api/v1/courses/99999')
        .expect(200);
    });
  });

  describe('Categories', () => {
    it('GET /api/v1/categories should return list', () => {
      return request(app.getHttpServer())
        .get('/api/v1/categories')
        .expect(200);
    });
  });

  describe('Prompts', () => {
    it('GET /api/v1/prompts should return paginated results', () => {
      return request(app.getHttpServer())
        .get('/api/v1/prompts')
        .expect(200)
        .expect((res) => {
          expect(res.body).toHaveProperty('items');
          expect(res.body).toHaveProperty('total');
        });
    });
  });

  describe('Community', () => {
    it('GET /api/v1/community/posts should return paginated results', () => {
      return request(app.getHttpServer())
        .get('/api/v1/community/posts')
        .expect(200)
        .expect((res) => {
          expect(res.body).toHaveProperty('items');
        });
    });
  });

  describe('Circles', () => {
    it('GET /api/v1/circles should return list', () => {
      return request(app.getHttpServer())
        .get('/api/v1/circles')
        .expect(200);
    });
  });

  describe('Search', () => {
    it('GET /api/v1/search should return results', () => {
      return request(app.getHttpServer())
        .get('/api/v1/search?q=AI')
        .expect(200)
        .expect((res) => {
          expect(res.body).toHaveProperty('results');
        });
    });
  });

  describe('Models', () => {
    it('GET /api/v1/models should return list', () => {
      return request(app.getHttpServer())
        .get('/api/v1/models')
        .expect(200);
    });
  });

  describe('Tools', () => {
    it('GET /api/v1/tools should return paginated results', () => {
      return request(app.getHttpServer())
        .get('/api/v1/tools')
        .expect(200)
        .expect((res) => {
          expect(res.body).toHaveProperty('items');
        });
    });
  });

  describe('Contents', () => {
    it('GET /api/v1/contents should return paginated results', () => {
      return request(app.getHttpServer())
        .get('/api/v1/contents')
        .expect(200);
    });
  });

  describe('Protected endpoints', () => {
    it('GET /api/v1/dashboard/stats should return 401 without token', () => {
      return request(app.getHttpServer())
        .get('/api/v1/dashboard/stats')
        .expect(401);
    });

    it('GET /api/v1/orders should return 401 without token', () => {
      return request(app.getHttpServer())
        .get('/api/v1/orders')
        .expect(401);
    });
  });
});
