const { getRequest } = require('../testServer');

describe('System Migration API', () => {
  describe('GET /api/system/migration/health', () => {
    it('should return 200 with healthy status', async () => {
      const response = await getRequest()
        .get('/api/system/migration/health')
        .expect('Content-Type', /json/)
        .expect(200);

      expect(response.body).toHaveProperty('database');
      expect(response.body.database).toHaveProperty('healthy');
      expect(response.body.database).toHaveProperty('status');
      expect(response.body.database.healthy).toBe(true);
    });

    it('should include database connection status', async () => {
      const response = await getRequest()
        .get('/api/system/migration/health');

      expect(response.body.database).toHaveProperty('healthy');
      expect(typeof response.body.database.healthy).toBe('boolean');
      expect(response.body.database.status).toBe('OK');
    });
  });

  describe('GET /api/system/migration/status', () => {
    it('should return current migration status', async () => {
      const response = await getRequest()
        .get('/api/system/migration/status')
        .expect('Content-Type', /json/)
        .expect(200);

      expect(response.body).toHaveProperty('database');
      expect(response.body).toHaveProperty('json');
      expect(response.body).toHaveProperty('migrated');
      expect(response.body).toHaveProperty('needsMigration');
      expect(typeof response.body.migrated).toBe('boolean');
    });

    it('should return migration metadata', async () => {
      const response = await getRequest()
        .get('/api/system/migration/status');

      expect(response.body.database).toHaveProperty('groups');
      expect(response.body.database).toHaveProperty('images');
      expect(response.body.database).toHaveProperty('initialized');
      expect(typeof response.body.database.groups).toBe('number');
      expect(typeof response.body.database.images).toBe('number');
    });
  });

  describe('POST /api/system/migration/migrate', () => {
    it('should require admin authorization', async () => {
      await getRequest()
        .post('/api/system/migration/migrate')
        .expect(403); // Should be protected by auth
    });
  });

  describe('POST /api/system/migration/rollback', () => {
    it('should require admin authorization', async () => {
      await getRequest()
        .post('/api/system/migration/rollback')
        .expect(403);
    });
  });
});
