import { beforeEach, describe, expect, it, vi } from 'vitest';

vi.mock('./db.js', () => ({
  completePairedTurn: vi.fn(),
  failPairedTurn: vi.fn(),
  getLastHumanMessageSender: vi.fn(() => '216851709744513024'),
  getLatestTurnNumber: vi.fn(() => 0),
  getPairedTaskById: vi.fn(),
  getPairedTurnOutputs: vi.fn(() => []),
  insertPairedTurnOutput: vi.fn(),
  refreshPairedTaskExecutionLease: vi.fn(() => true),
  releasePairedTaskExecutionLease: vi.fn(),
}));

vi.mock('./paired-execution-context.js', () => ({
  completePairedExecutionContext: vi.fn(),
}));

vi.mock('./paired-turn-run-ownership.js', () => ({
  resolvePairedTurnRunOwnership: vi.fn(() => ({ state: 'active' })),
}));

vi.mock('./message-runtime-follow-up.js', () => ({
  enqueuePairedFollowUpAfterEvent: vi.fn(),
}));

import type { AgentOutput } from './agent-runner.js';
import * as db from './db.js';
import * as pairedExecutionContextModule from './paired-execution-context.js';
import {
  createPairedExecutionLifecycle,
  resolveOwnerNextStepNotice,
} from './message-agent-executor-paired.js';
import type { PairedTask } from './types.js';

const log = {
  info: vi.fn(),
  warn: vi.fn(),
};

beforeEach(() => {
  vi.clearAllMocks();
});

describe('createPairedExecutionLifecycle output persistence', () => {
  it('stores final output attachments with the paired turn output', () => {
    const lifecycle = createPairedExecutionLifecycle({
      pairedExecutionContext: {
        task: {
          id: 'paired-task-output-attachment',
          chat_jid: 'group@test',
          group_folder: 'test-group',
          owner_service_id: 'codex-main',
          reviewer_service_id: 'claude',
          title: null,
          source_ref: 'HEAD',
          plan_notes: null,
          round_trip_count: 0,
          review_requested_at: null,
          status: 'active',
          arbiter_verdict: null,
          arbiter_requested_at: null,
          completion_reason: null,
          created_at: '2026-04-09T00:00:00.000Z',
          updated_at: '2026-04-09T00:00:00.000Z',
        },
        workspace: null,
        envOverrides: {},
      },
      pairedTurnIdentity: {
        turnId:
          'paired-task-output-attachment:2026-04-09T00:00:00.000Z:owner-turn',
        taskId: 'paired-task-output-attachment',
        taskUpdatedAt: '2026-04-09T00:00:00.000Z',
        intentKind: 'owner-turn',
        role: 'owner',
      },
      completedRole: 'owner',
      chatJid: 'group@test',
      runId: 'run-output-attachment',
      enqueueMessageCheck: vi.fn(),
      log,
    });

    lifecycle.recordFinalOutputBeforeDelivery('TASK_DONE\n새 렌더 첨부', [
      {
        path: '/tmp/settings-v0.1.92-deployed-390.png',
        name: 'settings-v0.1.92-deployed-390.png',
        mime: 'image/png',
      },
    ]);

    expect(db.insertPairedTurnOutput).toHaveBeenCalledWith(
      'paired-task-output-attachment',
      1,
      'owner',
      'TASK_DONE\n새 렌더 첨부',
      {
        attachments: [
          {
            path: '/tmp/settings-v0.1.92-deployed-390.png',
            name: 'settings-v0.1.92-deployed-390.png',
            mime: 'image/png',
          },
        ],
      },
    );
  });
});

describe('createPairedExecutionLifecycle verdict routing', () => {
  it('uses the full final output for paired verdict routing', () => {
    const lifecycle = createPairedExecutionLifecycle({
      pairedExecutionContext: {
        task: {
          id: 'paired-task-long-final',
          chat_jid: 'group@test',
          group_folder: 'test-group',
          owner_service_id: 'codex-main',
          reviewer_service_id: 'claude',
          title: null,
          source_ref: 'HEAD',
          plan_notes: null,
          round_trip_count: 0,
          review_requested_at: null,
          status: 'active',
          arbiter_verdict: null,
          arbiter_requested_at: null,
          completion_reason: null,
          created_at: '2026-04-09T00:00:00.000Z',
          updated_at: '2026-04-09T00:00:00.000Z',
        },
        workspace: null,
        envOverrides: {},
      },
      pairedTurnIdentity: {
        turnId: 'paired-task-long-final:2026-04-09T00:00:00.000Z:owner-turn',
        taskId: 'paired-task-long-final',
        taskUpdatedAt: '2026-04-09T00:00:00.000Z',
        intentKind: 'owner-turn',
        role: 'owner',
      },
      completedRole: 'owner',
      chatJid: 'group@test',
      runId: 'run-long-final',
      enqueueMessageCheck: vi.fn(),
      log,
    });
    const longPreface = '검증 증거 '.repeat(100);
    const finalOutput = `${longPreface}\nTASK_DONE\n뒤쪽 상태줄도 라우팅에 반영되어야 합니다.`;

    lifecycle.recordFinalOutputBeforeDelivery(finalOutput);

    expect(
      pairedExecutionContextModule.completePairedExecutionContext,
    ).toHaveBeenCalledWith(
      expect.objectContaining({
        taskId: 'paired-task-long-final',
        role: 'owner',
        status: 'succeeded',
        runId: 'run-long-final',
        summary: finalOutput,
      }),
    );
    expect(finalOutput.indexOf('TASK_DONE')).toBeGreaterThan(500);
  });
});

describe('createPairedExecutionLifecycle completion handling', () => {
  it('does not emit a second public notification after arbiter ESCALATE', async () => {
    const outputs: AgentOutput[] = [];

    vi.mocked(db.getPairedTaskById).mockReturnValue({
      id: 'paired-task-arbiter-escalated',
      chat_jid: 'group@test',
      group_folder: 'test-group',
      owner_service_id: 'claude',
      reviewer_service_id: 'codex-main',
      title: null,
      source_ref: 'HEAD',
      plan_notes: null,
      round_trip_count: 1,
      review_requested_at: '2026-04-09T00:00:00.000Z',
      status: 'completed',
      arbiter_verdict: 'escalate',
      arbiter_requested_at: '2026-04-09T00:00:00.000Z',
      completion_reason: 'arbiter_escalated',
      created_at: '2026-04-09T00:00:00.000Z',
      updated_at: '2026-04-09T00:00:01.000Z',
    });

    const lifecycle = createPairedExecutionLifecycle({
      pairedExecutionContext: {
        task: {
          id: 'paired-task-arbiter-escalated',
          chat_jid: 'group@test',
          group_folder: 'test-group',
          owner_service_id: 'claude',
          reviewer_service_id: 'codex-main',
          title: null,
          source_ref: 'HEAD',
          plan_notes: null,
          round_trip_count: 1,
          review_requested_at: '2026-04-09T00:00:00.000Z',
          status: 'in_arbitration',
          arbiter_verdict: null,
          arbiter_requested_at: '2026-04-09T00:00:00.000Z',
          completion_reason: null,
          created_at: '2026-04-09T00:00:00.000Z',
          updated_at: '2026-04-09T00:00:00.000Z',
        },
        workspace: null,
        envOverrides: {},
      },
      pairedTurnIdentity: {
        turnId:
          'paired-task-arbiter-escalated:2026-04-09T00:00:00.000Z:arbiter-turn',
        taskId: 'paired-task-arbiter-escalated',
        taskUpdatedAt: '2026-04-09T00:00:00.000Z',
        intentKind: 'arbiter-turn',
        role: 'arbiter',
      },
      completedRole: 'arbiter',
      chatJid: 'group@test',
      runId: 'run-arbiter-escalated',
      enqueueMessageCheck: vi.fn(),
      onOutput: async (output) => {
        outputs.push(output);
      },
      log,
    });

    lifecycle.recordFinalOutputBeforeDelivery(
      'ESCALATE\nuser decision required',
    );
    lifecycle.markStatus('succeeded');
    lifecycle.markSawOutput(true);
    await lifecycle.asyncFinalize();

    expect(outputs).toEqual([]);
  });

  it('delivers the held owner answer and a notice when the reviewer Codex is unavailable', async () => {
    const outputs: AgentOutput[] = [];

    vi.mocked(db.getPairedTurnOutputs).mockReturnValue([
      {
        id: 1,
        task_id: 'paired-task-reviewer-down',
        turn_number: 1,
        role: 'owner',
        output_text: 'TASK_DONE\n\n원하시던 .env 위치는 여기에 있습니다.',
        created_at: '2026-04-09T00:00:00.000Z',
      },
    ]);

    vi.mocked(db.getPairedTaskById).mockReturnValue({
      id: 'paired-task-reviewer-down',
      chat_jid: 'group@test',
      group_folder: 'test-group',
      owner_service_id: 'claude',
      reviewer_service_id: 'codex-main',
      title: null,
      source_ref: 'HEAD',
      plan_notes: null,
      round_trip_count: 1,
      review_requested_at: '2026-04-09T00:00:00.000Z',
      status: 'completed',
      arbiter_verdict: 'escalate',
      arbiter_requested_at: null,
      completion_reason: 'reviewer_codex_unavailable',
      created_at: '2026-04-09T00:00:00.000Z',
      updated_at: '2026-04-09T00:00:01.000Z',
    });

    const lifecycle = createPairedExecutionLifecycle({
      pairedExecutionContext: {
        task: {
          id: 'paired-task-reviewer-down',
          chat_jid: 'group@test',
          group_folder: 'test-group',
          owner_service_id: 'claude',
          reviewer_service_id: 'codex-main',
          title: null,
          source_ref: 'HEAD',
          plan_notes: null,
          round_trip_count: 1,
          review_requested_at: '2026-04-09T00:00:00.000Z',
          status: 'in_review',
          arbiter_verdict: null,
          arbiter_requested_at: null,
          completion_reason: null,
          created_at: '2026-04-09T00:00:00.000Z',
          updated_at: '2026-04-09T00:00:00.000Z',
        },
        workspace: null,
        envOverrides: {},
      },
      pairedTurnIdentity: {
        turnId:
          'paired-task-reviewer-down:2026-04-09T00:00:00.000Z:reviewer-turn',
        taskId: 'paired-task-reviewer-down',
        taskUpdatedAt: '2026-04-09T00:00:00.000Z',
        intentKind: 'reviewer-turn',
        role: 'reviewer',
      },
      completedRole: 'reviewer',
      chatJid: 'group@test',
      runId: 'run-reviewer-down',
      enqueueMessageCheck: vi.fn(),
      onOutput: async (output) => {
        outputs.push(output);
      },
      log,
    });

    lifecycle.markStatus('failed');
    lifecycle.updateSummary({
      errorText:
        'auth-expired: All Codex rotation accounts unavailable; re-auth required before launching Codex',
    });
    await lifecycle.asyncFinalize();

    const texts = outputs.map((o) =>
      o.output?.visibility === 'public' ? o.output.text : o.result,
    );
    expect(texts).toEqual([
      '원하시던 .env 위치는 여기에 있습니다.',
      'ℹ️ 리뷰어 일시 사용 불가 — 리뷰 없이 owner 답변으로 진행했습니다.',
    ]);
  });

  it('releases an owner turn interrupted by a human message without counting an owner failure', async () => {
    const enqueueMessageCheck = vi.fn();
    vi.mocked(db.getPairedTaskById).mockReturnValue({
      id: 'paired-task-human-interrupted',
      chat_jid: 'group@test',
      group_folder: 'test-group',
      owner_service_id: 'claude',
      reviewer_service_id: 'codex-main',
      title: null,
      source_ref: 'HEAD',
      plan_notes: null,
      round_trip_count: 1,
      review_requested_at: '2026-04-09T00:00:00.000Z',
      status: 'active',
      arbiter_verdict: null,
      arbiter_requested_at: null,
      completion_reason: null,
      created_at: '2026-04-09T00:00:00.000Z',
      updated_at: '2026-04-09T00:00:01.000Z',
    });

    const lifecycle = createPairedExecutionLifecycle({
      pairedExecutionContext: {
        task: {
          id: 'paired-task-human-interrupted',
          chat_jid: 'group@test',
          group_folder: 'test-group',
          owner_service_id: 'claude',
          reviewer_service_id: 'codex-main',
          title: null,
          source_ref: 'HEAD',
          plan_notes: null,
          round_trip_count: 1,
          review_requested_at: '2026-04-09T00:00:00.000Z',
          status: 'active',
          arbiter_verdict: null,
          arbiter_requested_at: null,
          completion_reason: null,
          created_at: '2026-04-09T00:00:00.000Z',
          updated_at: '2026-04-09T00:00:00.000Z',
        },
        workspace: null,
        envOverrides: {},
      },
      pairedTurnIdentity: {
        turnId:
          'paired-task-human-interrupted:2026-04-09T00:00:00.000Z:owner-turn',
        taskId: 'paired-task-human-interrupted',
        taskUpdatedAt: '2026-04-09T00:00:00.000Z',
        intentKind: 'owner-turn',
        role: 'owner',
      },
      completedRole: 'owner',
      chatJid: 'group@test',
      runId: 'run-human-interrupted',
      enqueueMessageCheck,
      getCloseReason: () => 'human-message-detected',
      log,
    });

    expect(
      lifecycle.recordFinalOutputBeforeDelivery(
        'TASK_DONE\n부분 진행 결과를 닫기 전에 내보냅니다.',
      ),
    ).toBe(false);
    lifecycle.updateSummary({
      outputText: '아비터 판단을 내리겠습니다.',
    });
    lifecycle.markStatus('succeeded');
    lifecycle.markSawOutput(false);
    await lifecycle.asyncFinalize();

    expect(
      pairedExecutionContextModule.completePairedExecutionContext,
    ).not.toHaveBeenCalled();
    expect(db.insertPairedTurnOutput).not.toHaveBeenCalled();
    expect(db.releasePairedTaskExecutionLease).toHaveBeenCalledWith({
      taskId: 'paired-task-human-interrupted',
      runId: 'run-human-interrupted',
    });
    expect(db.failPairedTurn).toHaveBeenCalledWith({
      turnIdentity: expect.objectContaining({
        taskId: 'paired-task-human-interrupted',
        role: 'owner',
      }),
      error: '아비터 판단을 내리겠습니다.',
    });
    expect(enqueueMessageCheck).not.toHaveBeenCalled();
  });
});

describe('resolveOwnerNextStepNotice routing indicator', () => {
  const task = (over: Partial<PairedTask>): PairedTask =>
    ({
      status: 'active',
      reviewer_agent_type: 'claude-code',
      completion_reason: null,
      ...over,
    }) as PairedTask;

  it('announces reviewer request while waiting', () => {
    expect(resolveOwnerNextStepNotice(task({ status: 'review_ready' }))).toBe(
      '🔁 리뷰어 검토 요청됨 — 리뷰어 응답을 기다리는 중입니다.',
    );
  });

  it('announces arbiter call when arbiter is requested', () => {
    expect(
      resolveOwnerNextStepNotice(task({ status: 'arbiter_requested' })),
    ).toBe('⚖️ 의견 차이로 중재자 호출됨 — 중재 판정을 기다리는 중입니다.');
  });

  it('stays silent when the task finishes (no redundant done line)', () => {
    expect(
      resolveOwnerNextStepNotice(
        task({ status: 'completed', completion_reason: 'done' }),
      ),
    ).toBeNull();
    expect(
      resolveOwnerNextStepNotice(
        task({ status: 'completed', completion_reason: 'escalated' }),
      ),
    ).toBeNull();
  });

  it('stays silent for in-progress owner work', () => {
    expect(resolveOwnerNextStepNotice(task({ status: 'active' }))).toBeNull();
  });
});
