/**
 * Tests for Human Checkpoints in ResourceRegistry
 */

import { describe, it, expect, beforeEach } from 'vitest'
import { ResourceRegistry } from './resource-registry'
import type { DeploymentSpec } from './resource-registry'
import type { HumanCheckpointDefinition } from './types'
import type { AgentDefinition, WorkflowDefinition } from '../../execution/engine'

// Mock agent
const mockAgent: AgentDefinition = {
  config: {
    resourceId: 'test-agent',
    name: 'Test Agent',
    description: 'Test agent',
    version: '1.0.0',
    type: 'agent',
    status: 'dev'
  },
  modelConfig: {
    provider: 'openai',
    model: 'gpt-5.4-mini',
    apiKey: 'test-key',
    temperature: 1
  },
  tools: [],
  systemPrompt: 'Test prompt'
}

// Mock workflow
const mockWorkflow: WorkflowDefinition = {
  config: {
    resourceId: 'test-workflow',
    name: 'Test Workflow',
    description: 'Test workflow',
    version: '1.0.0',
    type: 'workflow',
    status: 'dev'
  },
  entryPoint: 'start',
  steps: {}
}

describe('ResourceRegistry - Human Checkpoints', () => {
  let registry: ResourceRegistry

  beforeEach(() => {
    const humanCheckpoint: HumanCheckpointDefinition = {
      resourceId: 'test-approval',
      type: 'human',
      version: '1.0.0',
      name: 'Test Approval',
      description: 'Test human checkpoint',
      status: 'dev',
      requestedBy: {
        agents: ['test-agent']
      },
      routesTo: {
        workflows: ['test-workflow']
      }
    }

    const resources: DeploymentSpec = {
      agents: [mockAgent],
      workflows: [mockWorkflow],
      humanCheckpoints: [humanCheckpoint]
    }

    registry = new ResourceRegistry({
      'test-org': resources
    })
  })

  describe('getHumanCheckpoints', () => {
    it('should return all human checkpoints for an organization', () => {
      const humanCheckpoints = registry.getHumanCheckpoints('test-org')
      expect(humanCheckpoints).toHaveLength(1)
      expect(humanCheckpoints[0].resourceId).toBe('test-approval')
    })

    it('should return empty array for org without human checkpoints', () => {
      const humanCheckpoints = registry.getHumanCheckpoints('non-existent-org')
      expect(humanCheckpoints).toEqual([])
    })
  })

  describe('getHumanCheckpoint', () => {
    it('should return specific human checkpoint by ID', () => {
      const humanCheckpoint = registry.getHumanCheckpoint('test-org', 'test-approval')
      expect(humanCheckpoint).not.toBeNull()
      expect(humanCheckpoint?.resourceId).toBe('test-approval')
      expect(humanCheckpoint?.name).toBe('Test Approval')
    })

    it('should return null for non-existent human checkpoint', () => {
      const humanCheckpoint = registry.getHumanCheckpoint('test-org', 'non-existent')
      expect(humanCheckpoint).toBeNull()
    })
  })

  describe('serializeOrganization', () => {
    it('should include human checkpoints in command view data', () => {
      const commandView = registry.getCommandViewData('test-org')
      expect(commandView.humanCheckpoints).toHaveLength(1)
      expect(commandView.humanCheckpoints[0].resourceId).toBe('test-approval')
    })
  })

  describe('buildEdges', () => {
    it('should create approval edges from requestedBy', () => {
      const commandView = registry.getCommandViewData('test-org')
      const approvalEdges = commandView.edges.filter((e) => e.relationship === 'approval')
      expect(approvalEdges).toHaveLength(1)
      expect(approvalEdges[0].source).toBe('test-agent')
      expect(approvalEdges[0].target).toBe('test-approval')
    })

    it('should create triggers edges to routesTo', () => {
      const commandView = registry.getCommandViewData('test-org')
      const triggerEdges = commandView.edges.filter(
        (e) => e.source === 'test-approval' && e.relationship === 'triggers'
      )
      expect(triggerEdges).toHaveLength(1)
      expect(triggerEdges[0].source).toBe('test-approval')
      expect(triggerEdges[0].target).toBe('test-workflow')
    })
  })
})
