import fs from 'fs';
import path from 'path';

import type { AgentType } from './types.js';

const PLATFORM_PROMPT_FILES: Record<AgentType, string> = {
  'claude-code': 'claude-platform.md',
  codex: 'codex-platform.md',
};

const PAIRED_ROOM_PROMPT_FILES: Record<AgentType, string> = {
  'claude-code': 'claude-paired-room.md',
  codex: 'codex-paired-room.md',
};

export function getPlatformPromptsDir(projectRoot = process.cwd()): string {
  return path.join(projectRoot, 'prompts');
}

export function getPlatformPromptPath(
  agentType: AgentType,
  projectRoot = process.cwd(),
): string {
  return path.join(
    getPlatformPromptsDir(projectRoot),
    PLATFORM_PROMPT_FILES[agentType],
  );
}

export function readPlatformPrompt(
  agentType: AgentType,
  projectRoot = process.cwd(),
): string | undefined {
  const promptPath = getPlatformPromptPath(agentType, projectRoot);
  if (!fs.existsSync(promptPath)) return undefined;

  const prompt = fs.readFileSync(promptPath, 'utf-8').trim();
  return prompt || undefined;
}

export function getPairedRoomPromptPath(
  agentType: AgentType,
  projectRoot = process.cwd(),
): string {
  return path.join(
    getPlatformPromptsDir(projectRoot),
    PAIRED_ROOM_PROMPT_FILES[agentType],
  );
}

export function readPairedRoomPrompt(
  agentType: AgentType,
  projectRoot = process.cwd(),
): string | undefined {
  const promptPath = getPairedRoomPromptPath(agentType, projectRoot);
  if (!fs.existsSync(promptPath)) return undefined;

  const prompt = fs.readFileSync(promptPath, 'utf-8').trim();
  return prompt || undefined;
}
