// Transform orchestrator — runs DCP strategies in sequence.
// Clean-room implementation.

import type { DcpMessage } from './messages.js';
import { deduplicate } from './strategies/deduplication.js';
import { purgeErrors } from './strategies/purge-errors.js';
import { getDcpState, setDcpState, shouldTransform } from './state.js';

export interface TransformStats {
  removedCount: number;
  freedTokens: number;
  dedupRemoved: number;
  purgeRemoved: number;
}

export interface TransformResult {
  messages: DcpMessage[];
  stats: TransformStats;
}

export function transformMessages(chatId: string, messages: DcpMessage[]): TransformResult {
  if (!shouldTransform(chatId, messages.length)) {
    return { messages, stats: { removedCount: 0, freedTokens: 0, dedupRemoved: 0, purgeRemoved: 0 } };
  }

  let m = messages;

  // Step 1: Deduplicate
  const dedupResult = deduplicate(m);
  m = dedupResult.messages;
  const dedupRemoved = dedupResult.stats.removedCount;

  // Step 2: Purge errors
  const purgeResult = purgeErrors(m);
  m = purgeResult.messages;
  const purgeRemoved = purgeResult.stats.removedCount;

  const totalRemoved = dedupRemoved + purgeRemoved;
  const totalFreed = dedupResult.stats.freedTokens + purgeResult.stats.freedTokens;

  setDcpState(chatId, messages.length);

  return {
    messages: m,
    stats: {
      removedCount: totalRemoved,
      freedTokens: totalFreed,
      dedupRemoved,
      purgeRemoved,
    },
  };
}
