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

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

  beforeEach(async () => {
    app = Fastify({
      logger: false,
    });

    app.get('/health', async () => {
      return { status: 'ok', service: 'finance' };
    });

    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', 'finance');
    });
  });

  describe('POST /ledger/entry', () => {
    it('should require authentication', async () => {
      const response = await api.post('/ledger/entry', {
        accountId: 'test-account',
        type: 'debit',
        amount: 100,
        currency: 'USD',
      });

      expect([401, 500]).toContain(response.status);
    });
  });

  describe('POST /payments', () => {
    it('should require authentication', async () => {
      const response = await api.post('/payments', {
        amount: 100,
        currency: 'USD',
        paymentMethod: 'credit_card',
      });

      expect([401, 500]).toContain(response.status);
    });
  });
});

