/**
 * E2E Tests for Edge Cases
 *
 * Tests edge case scenarios in dispatch/coordination flow:
 * - Agent crashes during task
 * - Agent waiting for input
 * - Task blocking
 * - Merge conflicts
 *
 * Uses TestHarness from src/test/ for full system wiring.
 */

import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
import {
  createTestHarness,
  SIMPLE_FIXTURE,
  type TestHarness,
} from '../index.js';
import type {
  AgentSpawnedEvent,
  AgentCrashedEvent,
  AgentWaitingEvent,
  TaskBlockedEvent,
  MergeConflictedEvent,
} from '../../events/types.js';

describe('E2E Edge Cases', () => {
  let harness: TestHarness;

  beforeEach(() => {
    harness = createTestHarness();
  });

  afterEach(() => {
    harness.cleanup();
    vi.useRealTimers();
  });

  describe('Agent crash during task', () => {
    it('emits agent:spawned then agent:crashed events', async () => {
      vi.useFakeTimers();
      const seeded = await harness.seedFixture(SIMPLE_FIXTURE);
      const taskAId = seeded.tasks.get('Task A')!;

      // Pre-seed required idle agent for DispatchManager
      await harness.agentManager.spawn({
        name: 'pool-agent',
        taskId: 'placeholder',
        prompt: 'placeholder',
      });
      await harness.advanceTimers();

      // Set error scenario BEFORE dispatch
      harness.setAgentScenario(`agent-${taskAId.slice(0, 6)}`, {
        status: 'error',
        error: 'Token limit exceeded',
      });

      await harness.dispatchManager.queue(taskAId);
      harness.clearEvents();

      await harness.dispatchManager.dispatchNext();
      await harness.advanceTimers();

      // Verify: agent:spawned event emitted
      const spawnedEvents = harness.getEventsByType('agent:spawned');
      expect(spawnedEvents.length).toBe(1);
      const spawnedPayload = (spawnedEvents[0] as AgentSpawnedEvent).payload;
      expect(spawnedPayload.taskId).toBe(taskAId);

      // Verify: agent:crashed event emitted
      const crashedEvents = harness.getEventsByType('agent:crashed');
      expect(crashedEvents.length).toBe(1);
      const crashedPayload = (crashedEvents[0] as AgentCrashedEvent).payload;
      expect(crashedPayload.taskId).toBe(taskAId);
      expect(crashedPayload.error).toBe('Token limit exceeded');
    });

    it('task status should NOT be completed after crash', async () => {
      vi.useFakeTimers();
      const seeded = await harness.seedFixture(SIMPLE_FIXTURE);
      const taskAId = seeded.tasks.get('Task A')!;

      // Pre-seed required idle agent
      await harness.agentManager.spawn({
        name: 'pool-agent',
        taskId: 'placeholder',
        prompt: 'placeholder',
      });
      await harness.advanceTimers();

      // Set error scenario
      harness.setAgentScenario(`agent-${taskAId.slice(0, 6)}`, {
        status: 'error',
        error: 'Token limit exceeded',
      });

      await harness.dispatchManager.queue(taskAId);
      await harness.dispatchManager.dispatchNext();
      await harness.advanceTimers();

      // Task status should be 'in_progress' (not 'completed')
      const task = await harness.taskRepository.findById(taskAId);
      expect(task?.status).toBe('in_progress');
    });

    it('captures error message in agent result', async () => {
      vi.useFakeTimers();
      const seeded = await harness.seedFixture(SIMPLE_FIXTURE);
      const taskAId = seeded.tasks.get('Task A')!;

      // Pre-seed required idle agent
      await harness.agentManager.spawn({
        name: 'pool-agent',
        taskId: 'placeholder',
        prompt: 'placeholder',
      });
      await harness.advanceTimers();

      // Set error scenario
      harness.setAgentScenario(`agent-${taskAId.slice(0, 6)}`, {
        status: 'error',
        error: 'Out of memory',
      });

      await harness.dispatchManager.queue(taskAId);
      const dispatchResult = await harness.dispatchManager.dispatchNext();
      await harness.advanceTimers();

      // Get agent result - should have error
      const agentResult = await harness.agentManager.getResult(dispatchResult.agentId!);
      expect(agentResult).not.toBeNull();
      expect(agentResult?.success).toBe(false);
      expect(agentResult?.message).toBe('Out of memory');
    });
  });

  describe('Agent waiting for input and resume', () => {
    it('emits agent:waiting event with question', async () => {
      vi.useFakeTimers();
      const seeded = await harness.seedFixture(SIMPLE_FIXTURE);
      const taskAId = seeded.tasks.get('Task A')!;

      // Pre-seed required idle agent
      await harness.agentManager.spawn({
        name: 'pool-agent',
        taskId: 'placeholder',
        prompt: 'placeholder',
      });
      await harness.advanceTimers();

      // Set questions scenario
      harness.setAgentScenario(`agent-${taskAId.slice(0, 6)}`, {
        status: 'questions',
        questions: [{ id: 'q1', question: 'Which database should I use?' }],
      });

      await harness.dispatchManager.queue(taskAId);
      harness.clearEvents();

      await harness.dispatchManager.dispatchNext();
      await harness.advanceTimers();

      // Verify: agent:waiting event emitted
      const waitingEvents = harness.getEventsByType('agent:waiting');
      expect(waitingEvents.length).toBe(1);
      const waitingPayload = (waitingEvents[0] as AgentWaitingEvent).payload;
      expect(waitingPayload.taskId).toBe(taskAId);
      expect(waitingPayload.questions[0].question).toBe('Which database should I use?');
    });

  });

  describe('Task blocking', () => {
    it('blocked task appears in blocked list from getQueueState', async () => {
      const seeded = await harness.seedFixture(SIMPLE_FIXTURE);
      const taskAId = seeded.tasks.get('Task A')!;

      await harness.dispatchManager.queue(taskAId);
      await harness.dispatchManager.blockTask(taskAId, 'Waiting for user decision');

      const queueState = await harness.dispatchManager.getQueueState();

      expect(queueState.blocked.length).toBe(1);
      expect(queueState.blocked[0].taskId).toBe(taskAId);
      expect(queueState.blocked[0].reason).toBe('Waiting for user decision');
    });

    it('blocked task emits task:blocked event', async () => {
      const seeded = await harness.seedFixture(SIMPLE_FIXTURE);
      const taskAId = seeded.tasks.get('Task A')!;

      await harness.dispatchManager.queue(taskAId);
      harness.clearEvents();

      await harness.dispatchManager.blockTask(taskAId, 'Waiting for user decision');

      const blockedEvents = harness.getEventsByType('task:blocked');
      expect(blockedEvents.length).toBe(1);
      const blockedPayload = (blockedEvents[0] as TaskBlockedEvent).payload;
      expect(blockedPayload.taskId).toBe(taskAId);
      expect(blockedPayload.reason).toBe('Waiting for user decision');
    });

    it('getNextDispatchable does not return blocked task', async () => {
      vi.useFakeTimers();
      const seeded = await harness.seedFixture(SIMPLE_FIXTURE);
      const taskAId = seeded.tasks.get('Task A')!;
      const taskBId = seeded.tasks.get('Task B')!;

      // Pre-seed required idle agent
      await harness.agentManager.spawn({
        name: 'pool-agent',
        taskId: 'placeholder',
        prompt: 'placeholder',
      });
      await harness.advanceTimers();

      // Queue Task A and block it
      await harness.dispatchManager.queue(taskAId);
      await harness.dispatchManager.blockTask(taskAId, 'Blocked for testing');

      // Queue Task B (not blocked, but depends on Task A which needs to be completed first)
      // Actually Task B depends on Task A in SIMPLE_FIXTURE, but the dependency
      // isn't loaded into the queue. Queue a fresh task instead.
      // For this test, we just verify blocked task is not returned.

      // Get next dispatchable - should be null since Task A is blocked
      const next = await harness.dispatchManager.getNextDispatchable();
      expect(next).toBeNull();
    });

    it('task status is set to blocked in database', async () => {
      const seeded = await harness.seedFixture(SIMPLE_FIXTURE);
      const taskAId = seeded.tasks.get('Task A')!;

      await harness.dispatchManager.queue(taskAId);
      await harness.dispatchManager.blockTask(taskAId, 'Blocked for testing');

      const task = await harness.taskRepository.findById(taskAId);
      expect(task?.status).toBe('blocked');
    });
  });

  describe('Merge conflict handling', () => {
    it('detects merge conflict and emits merge:conflicted event', async () => {
      const seeded = await harness.seedFixture(SIMPLE_FIXTURE);
      const taskAId = seeded.tasks.get('Task A')!;

      // Mark task as completed (required for merge)
      await harness.taskRepository.update(taskAId, { status: 'completed' });

      // Create a worktree for this task
      const worktreeId = `wt-${taskAId.slice(0, 6)}`;
      await harness.worktreeManager.create(worktreeId, 'feature-task-a');

      // Create agent in agentRepository with worktreeId
      // (coordinationManager.queueMerge looks up agent by taskId)
      const agent = await harness.agentRepository.create({
        name: `agent-${taskAId.slice(0, 6)}`,
        worktreeId,
        taskId: taskAId,
        status: 'idle',
      });

      // Set up merge conflict result BEFORE processMerges
      harness.worktreeManager.setMergeResult(worktreeId, {
        success: false,
        conflicts: ['src/shared.ts', 'src/types.ts'],
        message: 'Merge conflict in 2 files',
      });

      // Queue for merge
      await harness.coordinationManager.queueMerge(taskAId);
      harness.clearEvents();

      // Process merges - should hit conflict
      const results = await harness.coordinationManager.processMerges('main');

      // Verify: merge result indicates failure
      expect(results.length).toBe(1);
      expect(results[0].success).toBe(false);
      expect(results[0].conflicts).toEqual(['src/shared.ts', 'src/types.ts']);

      // Verify: merge:conflicted event emitted
      const conflictEvents = harness.getEventsByType('merge:conflicted');
      expect(conflictEvents.length).toBe(1);
      const conflictPayload = (conflictEvents[0] as MergeConflictedEvent).payload;
      expect(conflictPayload.taskId).toBe(taskAId);
      expect(conflictPayload.conflictingFiles).toEqual(['src/shared.ts', 'src/types.ts']);
    });

    it('conflict appears in queue state as conflicted', async () => {
      const seeded = await harness.seedFixture(SIMPLE_FIXTURE);
      const taskAId = seeded.tasks.get('Task A')!;

      // Mark task as completed
      await harness.taskRepository.update(taskAId, { status: 'completed' });

      // Create worktree
      const worktreeId = `wt-${taskAId.slice(0, 6)}`;
      await harness.worktreeManager.create(worktreeId, 'feature-task-a');

      // Create agent in agentRepository
      await harness.agentRepository.create({
        name: `agent-${taskAId.slice(0, 6)}`,
        worktreeId,
        taskId: taskAId,
        status: 'idle',
      });

      // Set up merge conflict
      harness.worktreeManager.setMergeResult(worktreeId, {
        success: false,
        conflicts: ['src/shared.ts'],
        message: 'Merge conflict',
      });

      // Queue and process
      await harness.coordinationManager.queueMerge(taskAId);
      await harness.coordinationManager.processMerges('main');

      // Check queue state
      const queueState = await harness.coordinationManager.getQueueState();
      expect(queueState.conflicted.length).toBe(1);
      expect(queueState.conflicted[0].taskId).toBe(taskAId);
      expect(queueState.conflicted[0].conflicts).toContain('src/shared.ts');
    });

    it('handleConflict creates conflict-resolution task', async () => {
      const seeded = await harness.seedFixture(SIMPLE_FIXTURE);
      const taskAId = seeded.tasks.get('Task A')!;

      // Mark task as completed
      await harness.taskRepository.update(taskAId, { status: 'completed' });

      // Create worktree
      const worktreeId = `wt-${taskAId.slice(0, 6)}`;
      await harness.worktreeManager.create(worktreeId, 'feature-task-a');

      // Create agent in agentRepository
      await harness.agentRepository.create({
        name: `agent-${taskAId.slice(0, 6)}`,
        worktreeId,
        taskId: taskAId,
        status: 'idle',
      });

      // Set up merge conflict
      harness.worktreeManager.setMergeResult(worktreeId, {
        success: false,
        conflicts: ['src/shared.ts', 'src/types.ts'],
        message: 'Merge conflict',
      });

      // Queue and process (handleConflict is called automatically)
      await harness.coordinationManager.queueMerge(taskAId);
      await harness.coordinationManager.processMerges('main');

      // Verify: original task is now blocked
      const originalTask = await harness.taskRepository.findById(taskAId);
      expect(originalTask?.status).toBe('blocked');

      // Verify: task:queued event emitted for conflict resolution task
      const queuedEvents = harness.getEventsByType('task:queued');
      const conflictTaskEvent = queuedEvents.find(
        (e) => e.payload && (e.payload as { taskId: string }).taskId !== taskAId
      );
      expect(conflictTaskEvent).toBeDefined();
    });

    it('successful merge after clearing conflict result', async () => {
      const seeded = await harness.seedFixture(SIMPLE_FIXTURE);
      const taskAId = seeded.tasks.get('Task A')!;
      const taskBId = seeded.tasks.get('Task B')!;

      // Set up Task A for merge (with conflict)
      await harness.taskRepository.update(taskAId, { status: 'completed' });
      const worktreeIdA = `wt-${taskAId.slice(0, 6)}`;
      await harness.worktreeManager.create(worktreeIdA, 'feature-task-a');
      await harness.agentRepository.create({
        name: `agent-${taskAId.slice(0, 6)}`,
        worktreeId: worktreeIdA,
        taskId: taskAId,
        status: 'idle',
      });

      // Set conflict for Task A
      harness.worktreeManager.setMergeResult(worktreeIdA, {
        success: false,
        conflicts: ['src/shared.ts'],
        message: 'Merge conflict',
      });

      // Process Task A merge (will conflict)
      await harness.coordinationManager.queueMerge(taskAId);
      const conflictResults = await harness.coordinationManager.processMerges('main');
      expect(conflictResults[0].success).toBe(false);

      // Now set up Task B for merge (should succeed)
      await harness.taskRepository.update(taskBId, { status: 'completed' });
      const worktreeIdB = `wt-${taskBId.slice(0, 6)}`;
      await harness.worktreeManager.create(worktreeIdB, 'feature-task-b');
      await harness.agentRepository.create({
        name: `agent-${taskBId.slice(0, 6)}`,
        worktreeId: worktreeIdB,
        taskId: taskBId,
        status: 'idle',
      });

      // Task B merge should succeed (default behavior)
      await harness.coordinationManager.queueMerge(taskBId);
      harness.clearEvents();
      const successResults = await harness.coordinationManager.processMerges('main');

      // Verify Task B merged successfully
      expect(successResults.length).toBe(1);
      expect(successResults[0].taskId).toBe(taskBId);
      expect(successResults[0].success).toBe(true);

      // Verify Task B in merged list
      const queueState = await harness.coordinationManager.getQueueState();
      expect(queueState.merged).toContain(taskBId);
    });
  });
});
