/**
 * Integration Test: Document Workflow
 * Tests document creation, versioning, and workflow
 */

import { describe, it, expect, beforeAll, afterAll } from 'vitest';
import { setupTestContext, teardownTestContext, cleanupDatabase, TestContext } from './setup';

describe('Document Workflow - Integration', () => {
  let context: TestContext;

  beforeAll(async () => {
    context = await setupTestContext();
    await cleanupDatabase(context.dbPool);
  });

  afterAll(async () => {
    await cleanupDatabase(context.dbPool);
    await teardownTestContext(context);
  });

  describe('Document Lifecycle', () => {
    it('should create, update, and version a document', async () => {
      // 1. Create document via intake service
      const createResponse = await context.intakeService.inject({
        method: 'POST',
        url: '/api/v1/documents',
        payload: {
          title: 'Test Document',
          contentType: 'application/pdf',
          // Add other required fields
        },
      });

      expect(createResponse.statusCode).toBe(201);
      const document = createResponse.json();

      // 2. Update document
      const updateResponse = await context.intakeService.inject({
        method: 'PATCH',
        url: `/api/v1/documents/${document.id}`,
        payload: {
          title: 'Updated Test Document',
        },
      });

      expect(updateResponse.statusCode).toBe(200);

      // 3. Check version history
      const versionsResponse = await context.intakeService.inject({
        method: 'GET',
        url: `/api/v1/documents/${document.id}/versions`,
      });

      expect(versionsResponse.statusCode).toBe(200);
      const versions = versionsResponse.json();
      expect(versions).toHaveLength(2); // Original + update
    });
  });
});

