import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import Fastify, { FastifyInstance } from 'fastify';
import { createApiHelpers } from '@the-order/test-utils';

describe('Identity Service', () => {
  let app: FastifyInstance;
  let api: ReturnType<typeof createApiHelpers>;

  beforeEach(async () => {
    // Create a test instance of the server
    app = Fastify({
      logger: false,
    });

    // Import and register routes (simplified for testing)
    // In a real test, you'd want to import the actual server setup
    app.get('/health', async () => {
      return { status: 'ok', service: 'identity' };
    });

    await app.ready();
    api = createApiHelpers(app);
  });

  afterEach(async () => {
    if (app) {
      await app.close();
    }
  });

  describe('GET /health', () => {
    it('should return health status', async () => {
      const response = await api.get('/health');
      expect(response.status).toBe(200);
      expect(response.body).toHaveProperty('status');
      expect(response.body).toHaveProperty('service', 'identity');
    });
  });

  describe('POST /vc/issue', () => {
    it('should require authentication', async () => {
      const response = await api.post('/vc/issue', {
        subject: 'did:web:example.com',
        credentialSubject: {
          name: 'Test User',
        },
      });

      // Should return 401 without auth
      expect([401, 500]).toContain(response.status);
    });
  });

  describe('POST /vc/verify', () => {
    it('should require authentication', async () => {
      const response = await api.post('/vc/verify', {
        credential: {
          id: 'test-id',
        },
      });

      // Should return 401 without auth
      expect([401, 500]).toContain(response.status);
    });
  });
});
