/**
 * Document Versions Tests
 */

import { describe, it, expect, beforeEach } from 'vitest';
import {
  createDocumentVersion,
  getDocumentVersions,
  getLatestDocumentVersion,
  compareDocumentVersions,
  restoreDocumentVersion,
} from '@the-order/database';

describe('Document Versions', () => {
  const testDocumentId = 'test-doc-id';

  beforeEach(async () => {
    // Setup test data
  });

  it('should create a new document version', async () => {
    const version = await createDocumentVersion({
      document_id: testDocumentId,
      version_number: 1,
      content: 'Initial content',
      change_type: 'created',
    });

    expect(version).toBeDefined();
    expect(version.version_number).toBe(1);
    expect(version.content).toBe('Initial content');
  });

  it('should list all versions for a document', async () => {
    await createDocumentVersion({
      document_id: testDocumentId,
      version_number: 1,
      content: 'Version 1',
      change_type: 'created',
    });

    await createDocumentVersion({
      document_id: testDocumentId,
      version_number: 2,
      content: 'Version 2',
      change_type: 'modified',
    });

    const versions = await getDocumentVersions(testDocumentId);
    expect(versions.length).toBeGreaterThanOrEqual(2);
    expect(versions[0]?.version_number).toBeGreaterThanOrEqual(versions[1]?.version_number || 0);
  });

  it('should get the latest version', async () => {
    await createDocumentVersion({
      document_id: testDocumentId,
      version_number: 1,
      content: 'Version 1',
      change_type: 'created',
    });

    await createDocumentVersion({
      document_id: testDocumentId,
      version_number: 2,
      content: 'Version 2',
      change_type: 'modified',
    });

    const latest = await getLatestDocumentVersion(testDocumentId);
    expect(latest?.version_number).toBe(2);
  });

  it('should compare two versions', async () => {
    const v1 = await createDocumentVersion({
      document_id: testDocumentId,
      version_number: 1,
      content: 'Original content',
      change_type: 'created',
    });

    const v2 = await createDocumentVersion({
      document_id: testDocumentId,
      version_number: 2,
      content: 'Modified content',
      change_type: 'modified',
    });

    const comparison = await compareDocumentVersions(v1.id, v2.id);
    expect(comparison).toBeDefined();
    expect(comparison?.differences.length).toBeGreaterThan(0);
  });

  it('should restore a previous version', async () => {
    const original = await createDocumentVersion({
      document_id: testDocumentId,
      version_number: 1,
      content: 'Original content',
      change_type: 'created',
    });

    await createDocumentVersion({
      document_id: testDocumentId,
      version_number: 2,
      content: 'Modified content',
      change_type: 'modified',
    });

    const restored = await restoreDocumentVersion(
      testDocumentId,
      original.id,
      'test-user-id',
      'Restored from version 1'
    );

    expect(restored).toBeDefined();
    expect(restored.version_number).toBe(3);
    expect(restored.content).toBe('Original content');
  });
});

