const request = require('supertest');
const app = require('../src/app');

describe('Dubai DLD Analytics API', () => {
  
  describe('Health Check', () => {
    test('GET /health should return status OK', async () => {
      const response = await request(app)
        .get('/health')
        .expect(200);
      
      expect(response.body.status).toBe('OK');
      expect(response.body.service).toBe('Dubai DLD Analytics API');
    });
  });

  describe('Query Endpoints', () => {
    test('POST /api/query should process rental trend query', async () => {
      const query = {
        query: 'Give me the last 6 months rental price trend for Business Bay'
      };

      const response = await request(app)
        .post('/api/query')
        .send(query)
        .expect(200);

      expect(response.body.success).toBe(true);
      expect(response.body.data).toHaveProperty('text');
      expect(response.body.data).toHaveProperty('visualizations');
      expect(response.body.data).toHaveProperty('cards');
      expect(response.body.data).toHaveProperty('sql_queries');
    });

    test('GET /api/queries/top-areas should return top areas', async () => {
      const response = await request(app)
        .get('/api/queries/top-areas')
        .expect(200);

      expect(response.body.success).toBe(true);
      expect(response.body.data).toHaveProperty('visualizations');
    });

    test('GET /api/queries/project-summary should return project data', async () => {
      const response = await request(app)
        .get('/api/queries/project-summary')
        .expect(200);

      expect(response.body.success).toBe(true);
      expect(response.body.data).toHaveProperty('text');
    });
  });

  describe('Validation', () => {
    test('POST /api/query should validate query length', async () => {
      const invalidQuery = {
        query: 'ab' // Too short
      };

      const response = await request(app)
        .post('/api/query')
        .send(invalidQuery)
        .expect(400);

      expect(response.body.success).toBe(false);
      expect(response.body.message).toBe('Validation error');
    });

    test('POST /api/query should require query field', async () => {
      const response = await request(app)
        .post('/api/query')
        .send({})
        .expect(400);

      expect(response.body.success).toBe(false);
    });
  });

  describe('Error Handling', () => {
    test('GET /api/nonexistent should return 404', async () => {
      const response = await request(app)
        .get('/api/nonexistent')
        .expect(404);

      expect(response.body.success).toBe(false);
      expect(response.body.message).toBe('Endpoint not found');
    });
  });
});

