import { readFile } from 'fs/promises';
import path from 'path';
import { LoggerService } from '../core/logger.service.js';
export class PromptTemplateService {
    templatesPath;
    logger = LoggerService.getInstance().createComponentLogger('PromptTemplateService');
    constructor(templatesPath) {
        this.templatesPath = templatesPath || path.join(process.cwd(), 'config', 'orchestrator_tasks', 'prompts');
    }
    /**
     * Load and process orchestrator task assignment template
     */
    async getOrchestratorTaskAssignmentPrompt(data) {
        const templatePath = path.join(this.templatesPath, 'assign-task-orchestrator-prompt-template.md');
        const template = await readFile(templatePath, 'utf-8');
        // Ensure all required fields have values - CRITICAL for orchestrator template paths
        const processedData = {
            ...data,
            taskDescription: data.taskDescription || 'No description provided',
            taskPriority: data.taskPriority || 'medium',
            taskMilestone: data.taskMilestone || 'general',
            projectPath: data.projectPath || '[MISSING_PROJECT_PATH]',
            taskId: data.taskId || '[MISSING_TASK_ID]'
        };
        // Log warning if critical orchestrator template values are missing
        if (!data.projectPath || !data.taskMilestone || !data.taskId) {
            this.logger.warn('Orchestrator template missing critical values', {
                projectPath: data.projectPath || 'MISSING',
                taskMilestone: data.taskMilestone || 'MISSING',
                taskId: data.taskId || 'MISSING',
                taskTitle: data.taskTitle || 'MISSING'
            });
        }
        return this.processTemplate(template, processedData);
    }
    /**
     * Extract and process team member task assignment prompt from orchestrator template
     */
    async getTeamMemberTaskAssignmentPrompt(data) {
        // Load the orchestrator template and extract the team member message
        const orchestratorTemplate = await this.getOrchestratorTaskAssignmentPrompt(data);
        // Extract the message content from the send_message call
        const messageMatch = orchestratorTemplate.match(/message: "([^"]*(?:\\"[^"]*)*?)"/s);
        if (messageMatch) {
            return messageMatch[1].replace(/\\"/g, '"'); // Unescape quotes
        }
        // Fallback if extraction fails
        return `📋 TASK ASSIGNMENT - ${data.taskTitle}

**Task File:** \`${data.projectPath}/.crewly/tasks/${data.taskMilestone || 'general'}/open/${data.taskId}.md\`
**Priority:** ${data.taskPriority || 'medium'}

Please:
1. Read the complete task file above for full specifications
2. Call accept_task to move it to in_progress:
   accept_task({ absoluteTaskPath: '${data.projectPath}/.crewly/tasks/${data.taskMilestone || 'general'}/open/${data.taskId}.md', memberId: '[your_member_id]' })
3. Follow exact deliverables and file locations specified in the task file

CRITICAL: Read the actual task file, not this summary!`;
    }
    /**
     * Load and process project start orchestrator template for immediate coordination
     */
    async getProjectStartPrompt(data) {
        const templatePath = path.join(this.templatesPath, 'project-start-orchestrator-prompt-template.md');
        const template = await readFile(templatePath, 'utf-8');
        return this.processTemplate(template, data);
    }
    /**
     * Load and process check-in prompt for 15-minute cycles (includes auto-assignment)
     */
    async getCheckinPrompt(data) {
        const templatePath = path.join(this.templatesPath, 'checkin-orchestrator-prompt-template.md');
        const template = await readFile(templatePath, 'utf-8');
        return this.processTemplate(template, data);
    }
    /**
     * Process template by replacing placeholders with actual values
     */
    processTemplate(template, data) {
        let processed = template;
        // Replace all {key} placeholders with values
        Object.entries(data).forEach(([key, value]) => {
            const placeholder = new RegExp(`{${key}}`, 'g');
            processed = processed.replace(placeholder, value || '');
        });
        // Validate that critical path components are present for orchestrator templates
        if (template.includes('**Task File Location:**')) {
            const { projectPath, taskMilestone, taskId } = data;
            if (!projectPath || !taskMilestone || !taskId) {
                this.logger.warn('Orchestrator template missing critical path components', {
                    hasProjectPath: !!projectPath,
                    hasTaskMilestone: !!taskMilestone,
                    hasTaskId: !!taskId
                });
            }
        }
        return processed;
    }
}
//# sourceMappingURL=prompt-template.service.js.map