/**
 * Prompt Template Utilities | 提示词模板工具
 * 
 * 统一前端模板拼接逻辑，用于：
 * 1. PromptGeneratorRenderer 预览
 * 2. canvasService 发送给后端前的拼接
 */

// Template definitions | 模板定义
export interface TemplateDefinition {
    value: string;
    label: string;
    targetType: 'image' | 'video' | 'both';
    // Template builder function | 模板构建函数
    build: (userPrompt: string, context?: string) => string;
}

/**
 * Image Templates | 图片模板
 */
export const IMAGE_TEMPLATE_BUILDERS: Record<string, (userPrompt: string, context?: string) => string> = {
    'general': (userPrompt) => userPrompt,
    
    'character_white_bg': (userPrompt) => 
        `${userPrompt}

Requirements:
- Pure white background (#FFFFFF)
- Full body character portrait
- High detail, clean edges suitable for cutout
- Professional studio lighting
- Clear silhouette`,

    'character_three_view': (userPrompt) => 
        `${userPrompt}

Requirements:
- Character reference sheet with three views: front, side (3/4), back
- Pure white background
- Consistent proportions across all views
- Clear anatomical details
- Design reference purpose`,

    'storyboard_integrated': (userPrompt, context) => {
        if (context) {
            return `Reference Assets:
${context}

Shot Description:
${userPrompt}`;
        }
        return userPrompt;
    },
};

/**
 * Video Templates | 视频模板
 */
export const VIDEO_TEMPLATE_BUILDERS: Record<string, (userPrompt: string, context?: string) => string> = {
    'general': (userPrompt) => userPrompt,
    
    'asset_360': (userPrompt) => 
        `${userPrompt}

Camera: Smooth 360-degree orbital rotation around the subject
Movement: Slow, steady turntable motion
Lighting: Professional studio lighting highlighting texture and form
Background: Clean, non-distracting
Duration: Complete rotation cycle`,

    'storyboard_video_integrated': (userPrompt, context) => {
        if (context) {
            return `Reference Assets:
${context}

Action Description:
${userPrompt}

Requirements:
- Clear motion and action
- Cinematic camera movement
- Consistent character appearance`;
        }
        return userPrompt;
    },
};

/**
 * Build prompt with template | 使用模板构建提示词
 * 
 * @param userPrompt - User's original input | 用户原始输入
 * @param template - Template name | 模板名称
 * @param targetType - Target type (image/video) | 目标类型
 * @param context - Optional upstream context | 可选的上游上下文
 * @returns Formatted prompt | 格式化后的提示词
 */
export function buildPromptWithTemplate(
    userPrompt: string,
    template: string | undefined,
    targetType: 'image' | 'video' = 'image',
    context?: string
): string {
    if (!userPrompt && !context) return '';
    
    const effectiveTemplate = template || 'general';
    
    // Select builder based on target type
    const builders = targetType === 'video' 
        ? VIDEO_TEMPLATE_BUILDERS 
        : IMAGE_TEMPLATE_BUILDERS;
    
    const builder = builders[effectiveTemplate];
    
    if (builder) {
        return builder(userPrompt || '', context);
    }
    
    // Fallback to general
    return userPrompt || '';
}

/**
 * Check if template requires context | 检查模板是否需要上下文
 */
export function templateRequiresContext(template: string | undefined): boolean {
    const contextTemplates = ['storyboard_integrated', 'storyboard_video_integrated'];
    return contextTemplates.includes(template || '');
}

/**
 * Get template label | 获取模板标签
 */
export function getTemplateLabel(
    template: string | undefined, 
    targetType: 'image' | 'video' = 'image'
): string {
    const imageLabels: Record<string, string> = {
        'general': '通用',
        'character_white_bg': '角色白底图',
        'character_three_view': '角色三视图',
        'storyboard_integrated': '分镜出图',
    };
    
    const videoLabels: Record<string, string> = {
        'general': '通用',
        'asset_360': '360度展示',
        'storyboard_video_integrated': '分镜视频',
    };
    
    const labels = targetType === 'video' ? videoLabels : imageLabels;
    return labels[template || 'general'] || '通用';
}
