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

describe('Consent Management API', () => {
  let adminSession;

  beforeAll(async () => {
    adminSession = await getAdminSession();
  });

  describe('GET /api/admin/social-media/platforms', () => {
    it('should return list of social media platforms', async () => {
      const response = await adminSession.agent
        .get('/api/admin/social-media/platforms')
        .expect('Content-Type', /json/)
        .expect(200);

      expect(Array.isArray(response.body)).toBe(true);
    });

    it('should include platform metadata', async () => {
      const response = await adminSession.agent
        .get('/api/admin/social-media/platforms');

      if (response.body.length > 0) {
        const platform = response.body[0];
        expect(platform).toHaveProperty('id');
        expect(platform).toHaveProperty('platform_name');
        expect(platform).toHaveProperty('display_name');
      }
    });
  });

  describe('GET /api/admin/groups/:groupId/consents', () => {
    it('should return 404 for non-existent group', async () => {
      await adminSession.agent
        .get('/api/admin/groups/non-existent-group/consents')
        .expect(404);
    });

    it('should reject path traversal attempts', async () => {
      await adminSession.agent
        .get('/api/admin/groups/../../../etc/passwd/consents')
        .expect(404);
    });
  });

  describe('POST /api/admin/groups/:groupId/consents', () => {
    it('should require admin authorization', async () => {
      await getRequest()
        .post('/api/admin/groups/test-group-id/consents')
        .send({ consents: {} })
        .expect(403); // No auth header
    });

    it('should require valid consent data with auth', async () => {
      const response = await adminSession.agent
        .post('/api/admin/groups/test-group-id/consents')
        .set('X-CSRF-Token', adminSession.csrfToken)
        .send({})
        .expect(400);

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

  describe('GET /api/admin/groups/by-consent', () => {
    it('should return filtered groups', async () => {
      const response = await adminSession.agent
        .get('/api/admin/groups/by-consent')
        .expect('Content-Type', /json/)
        .expect(200);

      expect(response.body).toHaveProperty('groups');
      expect(response.body).toHaveProperty('count');
      expect(Array.isArray(response.body.groups)).toBe(true);
    });

    it('should accept platform filter', async () => {
      const response = await adminSession.agent
        .get('/api/admin/groups/by-consent?platformId=1')
        .expect(200);

      expect(response.body).toHaveProperty('groups');
      expect(response.body).toHaveProperty('filters');
    });

    it('should accept consent filter', async () => {
      const response = await adminSession.agent
        .get('/api/admin/groups/by-consent?displayInWorkshop=true')
        .expect(200);

      expect(response.body).toHaveProperty('groups');
      expect(response.body.filters).toHaveProperty('displayInWorkshop', true);
    });
  });

  describe('GET /api/admin/consents/export', () => {
    it('should require admin authorization', async () => {
      await getRequest()
        .get('/api/admin/consents/export')
        .expect(403);
    });

    it('should return CSV format with auth and format parameter', async () => {
      const response = await adminSession.agent
        .get('/api/admin/consents/export?format=csv')
        .expect(200);

      expect(response.headers['content-type']).toMatch(/text\/csv/);
      expect(response.headers['content-disposition']).toMatch(/attachment/);
    });

    it('should include CSV header', async () => {
      const response = await adminSession.agent
        .get('/api/admin/consents/export?format=csv');

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