import * as request from 'supertest';
import { INestApplication } from '@nestjs/common';

describe('Leaderboard Service E2E Tests', () => {
  let app: INestApplication;

  beforeAll(() => {
    app = global.testApp;
  });

  describe('Health Check', () => {
    it('/health (GET) - should return health status', async () => {
      if (!app) {
        console.log('Skipping E2E test - app not initialized');
        return;
      }

      const response = await request(app.getHttpServer())
        .get('/health')
        .expect(200);

      expect(response.body).toHaveProperty('status');
      expect(response.body.status).toBe('ok');
    });

    it('/health/ready (GET) - should return readiness status', async () => {
      if (!app) {
        console.log('Skipping E2E test - app not initialized');
        return;
      }

      const response = await request(app.getHttpServer())
        .get('/health/ready')
        .expect(200);

      expect(response.body).toHaveProperty('status');
    });
  });

  describe('Leaderboard API', () => {
    describe('GET /leaderboard/daily', () => {
      it('should return daily leaderboard (public)', async () => {
        if (!app) {
          console.log('Skipping E2E test - app not initialized');
          return;
        }

        const response = await request(app.getHttpServer())
          .get('/leaderboard/daily')
          .expect(200);

        expect(response.body).toBeDefined();
        expect(Array.isArray(response.body.rankings) || response.body.rankings === undefined).toBe(true);
      });
    });

    describe('GET /leaderboard/weekly', () => {
      it('should return weekly leaderboard (public)', async () => {
        if (!app) {
          console.log('Skipping E2E test - app not initialized');
          return;
        }

        const response = await request(app.getHttpServer())
          .get('/leaderboard/weekly')
          .expect(200);

        expect(response.body).toBeDefined();
      });
    });

    describe('GET /leaderboard/monthly', () => {
      it('should return monthly leaderboard (public)', async () => {
        if (!app) {
          console.log('Skipping E2E test - app not initialized');
          return;
        }

        const response = await request(app.getHttpServer())
          .get('/leaderboard/monthly')
          .expect(200);

        expect(response.body).toBeDefined();
      });
    });
  });

  describe('Authentication Protected Routes', () => {
    describe('GET /leaderboard/my-rank', () => {
      it('should return 401 without authentication', async () => {
        if (!app) {
          console.log('Skipping E2E test - app not initialized');
          return;
        }

        await request(app.getHttpServer())
          .get('/leaderboard/my-rank')
          .expect(401);
      });
    });
  });

  describe('Admin Protected Routes', () => {
    describe('GET /leaderboard/config', () => {
      it('should return 401 without authentication', async () => {
        if (!app) {
          console.log('Skipping E2E test - app not initialized');
          return;
        }

        await request(app.getHttpServer())
          .get('/leaderboard/config')
          .expect(401);
      });
    });

    describe('POST /leaderboard/config/switch', () => {
      it('should return 401 without authentication', async () => {
        if (!app) {
          console.log('Skipping E2E test - app not initialized');
          return;
        }

        await request(app.getHttpServer())
          .post('/leaderboard/config/switch')
          .send({ type: 'daily', enabled: true })
          .expect(401);
      });
    });

    describe('GET /virtual-accounts', () => {
      it('should return 401 without authentication', async () => {
        if (!app) {
          console.log('Skipping E2E test - app not initialized');
          return;
        }

        await request(app.getHttpServer())
          .get('/virtual-accounts')
          .expect(401);
      });
    });
  });

  describe('Swagger Documentation', () => {
    it('/api-docs (GET) - should return swagger UI', async () => {
      if (!app) {
        console.log('Skipping E2E test - app not initialized');
        return;
      }

      const response = await request(app.getHttpServer())
        .get('/api-docs')
        .expect(200);

      expect(response.text).toContain('html');
    });

    it('/api-docs-json (GET) - should return swagger JSON', async () => {
      if (!app) {
        console.log('Skipping E2E test - app not initialized');
        return;
      }

      const response = await request(app.getHttpServer())
        .get('/api-docs-json')
        .expect(200);

      expect(response.body).toHaveProperty('openapi');
      expect(response.body).toHaveProperty('info');
      expect(response.body.info.title).toContain('Leaderboard');
    });
  });
});
