import { describe, it, expect, beforeEach } from 'vitest'
import { versionControlService, type Version } from '../versionControl'

const mockTopology = {
  id: 'test-topology',
  region: 'Europe',
  entity: 'SMOM',
  nodes: [{ id: 'node-1', type: 'vm', label: 'VM 1', position: { x: 0, y: 0 } }],
  edges: [],
  lastUpdated: new Date().toISOString(),
}

describe('versionControlService', () => {
  beforeEach(() => {
    // Clear versions (would need to add clear method or reset)
  })

  it('should save a version', () => {
    const version = versionControlService.saveVersion(mockTopology, null)

    expect(version).toBeDefined()
    expect(version.entityId).toBe('test-topology')
    expect(version.entityType).toBe('topology')
    expect(version.version).toBe(1)
    expect(version.data).toEqual(mockTopology)
    expect(version.changes).toHaveLength(0) // No previous version
  })

  it('should track changes between versions', () => {
    const v1 = { ...mockTopology, region: 'Europe' }
    const v2 = { ...mockTopology, region: 'Asia' }

    versionControlService.saveVersion(v1, null)
    const version2 = versionControlService.saveVersion(v2, v1)

    expect(version2.changes.length).toBeGreaterThan(0)
    const regionChange = version2.changes.find((c) => c.field === 'region')
    expect(regionChange).toBeDefined()
    expect(regionChange?.before).toBe('Europe')
    expect(regionChange?.after).toBe('Asia')
  })

  it('should get versions for an entity', () => {
    versionControlService.saveVersion(mockTopology, null)
    versionControlService.saveVersion({ ...mockTopology, region: 'Asia' }, mockTopology)

    const versions = versionControlService.getVersions('test-topology')
    expect(versions).toHaveLength(2)
    expect(versions[0].version).toBe(1)
    expect(versions[1].version).toBe(2)
  })

  it('should get specific version', () => {
    versionControlService.saveVersion(mockTopology, null)
    versionControlService.saveVersion({ ...mockTopology, region: 'Asia' }, mockTopology)

    const version = versionControlService.getVersion('test-topology', 1)
    expect(version).toBeDefined()
    expect(version?.version).toBe(1)
    expect(version?.data.region).toBe('Europe')
  })

  it('should get latest version', () => {
    versionControlService.saveVersion(mockTopology, null)
    versionControlService.saveVersion({ ...mockTopology, region: 'Asia' }, mockTopology)

    const latest = versionControlService.getLatestVersion('test-topology')
    expect(latest).toBeDefined()
    expect(latest?.version).toBe(2)
    expect(latest?.data.region).toBe('Asia')
  })

  it('should compare versions', () => {
    const v1 = { ...mockTopology, region: 'Europe', nodes: [] }
    const v2 = { ...mockTopology, region: 'Asia', nodes: [{ id: 'new-node' }] }

    versionControlService.saveVersion(v1, null)
    versionControlService.saveVersion(v2, v1)

    const comparison = versionControlService.compareVersions('test-topology', 1, 2)

    expect(comparison.modified.length).toBeGreaterThan(0)
    const regionChange = comparison.modified.find((c) => c.field === 'region')
    expect(regionChange).toBeDefined()
  })

  it('should restore a version', () => {
    const v1 = { ...mockTopology, region: 'Europe' }
    versionControlService.saveVersion(v1, null)
    versionControlService.saveVersion({ ...mockTopology, region: 'Asia' }, v1)

    const restored = versionControlService.restoreVersion('test-topology', 1)
    expect(restored).toBeDefined()
    expect((restored as any).region).toBe('Europe')
  })
})

