/**
 * Context API Routes
 *
 * 上下文压缩相关的 REST API
 */

import { Hono } from 'hono';
import { getSessionManager } from '../session/manager.js';
import {
  getContextUsage,
  compressContext,
  type ContextUsageInfo,
  type CompressionResult,
} from '../agent/adapter.js';

export const contextRouter = new Hono();

/**
 * GET /sessions/:id/context - 获取会话上下文使用情况
 */
contextRouter.get('/sessions/:id/context', (c) => {
  const sessionId = c.req.param('id');
  const sessionManager = getSessionManager();

  // 验证会话存在
  if (!sessionManager.exists(sessionId)) {
    return c.json(
      {
        success: false,
        error: 'Session not found',
      },
      404
    );
  }

  const usage = getContextUsage(sessionId);

  if (!usage) {
    // Agent 未初始化，返回默认值
    return c.json({
      success: true,
      data: {
        input: 0,
        contextLimit: 200000,
        available: 180000,
        usagePercent: 0,
        formatted: '0/180K (0%)',
        shouldCompress: false,
      } as ContextUsageInfo,
    });
  }

  return c.json({
    success: true,
    data: usage,
  });
});

/**
 * POST /sessions/:id/compress - 触发手动压缩
 */
contextRouter.post('/sessions/:id/compress', async (c) => {
  const sessionId = c.req.param('id');
  const sessionManager = getSessionManager();

  // 验证会话存在
  if (!sessionManager.exists(sessionId)) {
    return c.json(
      {
        success: false,
        error: 'Session not found',
      },
      404
    );
  }

  try {
    const body = await c.req.json().catch(() => ({}));
    const force = body.force === true;

    const result = await compressContext(sessionId, force);

    if (!result) {
      return c.json(
        {
          success: false,
          error: 'Agent not initialized for this session',
        },
        400
      );
    }

    return c.json({
      success: true,
      data: result,
    });
  } catch (error) {
    return c.json(
      {
        success: false,
        error: error instanceof Error ? error.message : 'Compression failed',
      },
      500
    );
  }
});
