/**
 * End-to-end tests for Admin Panel
 * Run with: pnpm test
 */

import request from 'supertest';
import { describe, it, expect, beforeAll, afterAll } from '@jest/globals';

// Note: This requires the server to be running
// For full E2E tests, use Playwright or Cypress

describe('Admin Panel API', () => {
  const baseUrl = process.env.TEST_URL || 'http://localhost:5000';
  let adminToken: string;

  beforeAll(async () => {
    // Login to get admin token
    const loginResponse = await request(baseUrl)
      .post('/api/admin/login')
      .send({ username: 'admin', password: 'admin' });
    
    if (loginResponse.status === 200) {
      adminToken = loginResponse.body.token;
    }
  });

  describe('Authentication', () => {
    it('should login with valid credentials', async () => {
      const response = await request(baseUrl)
        .post('/api/admin/login')
        .send({ username: 'admin', password: 'admin' });
      
      expect(response.status).toBe(200);
      expect(response.body).toHaveProperty('token');
      expect(response.body).toHaveProperty('username');
    });

    it('should reject invalid credentials', async () => {
      const response = await request(baseUrl)
        .post('/api/admin/login')
        .send({ username: 'admin', password: 'wrong' });
      
      expect(response.status).toBe(401);
    });

    it('should require authentication for admin routes', async () => {
      const response = await request(baseUrl)
        .get('/api/admin/services');
      
      expect(response.status).toBe(401);
    });
  });

  describe('Service Management', () => {
    it('should get all services', async () => {
      const response = await request(baseUrl)
        .get('/api/admin/services')
        .set('X-Admin-Token', adminToken);
      
      expect(response.status).toBe(200);
      expect(Array.isArray(response.body)).toBe(true);
    });

    it('should update service configuration', async () => {
      const serviceName = 'test-service';
      const response = await request(baseUrl)
        .put(`/api/admin/services/${serviceName}`)
        .set('X-Admin-Token', adminToken)
        .send({ enabled: true });
      
      expect(response.status).toBe(200);
      expect(response.body).toHaveProperty('success', true);
    });
  });

  describe('Provider Management', () => {
    it('should get all providers', async () => {
      const response = await request(baseUrl)
        .get('/api/admin/providers')
        .set('X-Admin-Token', adminToken);
      
      expect(response.status).toBe(200);
      expect(Array.isArray(response.body)).toBe(true);
    });

    it('should update provider configuration', async () => {
      const providerName = 'azure';
      const response = await request(baseUrl)
        .put(`/api/admin/providers/${providerName}`)
        .set('X-Admin-Token', adminToken)
        .send({ enabled: true });
      
      expect(response.status).toBe(200);
      expect(response.body).toHaveProperty('success', true);
    });
  });

  describe('Environment Management', () => {
    it('should toggle environment', async () => {
      const envName = 'workload-azure-eastus';
      const response = await request(baseUrl)
        .put(`/api/admin/environments/${envName}/toggle`)
        .set('X-Admin-Token', adminToken)
        .send({ enabled: true });
      
      // May return 404 if environment doesn't exist, or 200 if it does
      expect([200, 404]).toContain(response.status);
    });
  });

  describe('Audit Logs', () => {
    it('should get audit logs', async () => {
      const response = await request(baseUrl)
        .get('/api/admin/audit-logs?limit=10')
        .set('X-Admin-Token', adminToken);
      
      expect(response.status).toBe(200);
      expect(Array.isArray(response.body)).toBe(true);
    });
  });
});

