import { NodeExecutionStrategy, ExecutionContext } from '../types';
import { analyzeVideo, urlToBase64 } from '../../canvasService';
import { AppNodeData } from '../../../types/node';

export class VideoAnalyzerStrategy implements NodeExecutionStrategy {
    async execute(context: ExecutionContext): Promise<void> {
        const { 
            nodeId, node, inputs, prompt, 
            updateNodeData, getLLMModelDefault
        } = context;

        if (node.type !== 'appNode') return;
        const nodeData = node.data as AppNodeData;

        const vid = nodeData.videoUri || (inputs.find(n => n.type === 'appNode' && (n.data as AppNodeData).videoUri)?.data as AppNodeData | undefined)?.videoUri;
        if (!vid) throw new Error("未找到视频输入");
        
        let vidData = vid;
        if (vid.startsWith('http')) vidData = await urlToBase64(vid);
        
        const txt = await analyzeVideo(vidData, prompt, node.data.model || getLLMModelDefault());
        updateNodeData(nodeId, { analysis: txt, status: 'SUCCESS' });
    }
}
