package com.yaoai.ai.providers.service.impl;

import com.yaoai.ai.core.model.ChatMessage;
import com.yaoai.ai.core.model.ChatRequest;
import com.yaoai.ai.core.service.LlmService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;

import java.util.stream.Collectors;

@Slf4j
@Service
@Profile("dev-h2")
@ConditionalOnProperty(name = "ai.local-stub.enabled", havingValue = "true")
public class LocalLlmService implements LlmService {

    @Override
    public String chat(ChatRequest request) {
        String prompt = request.getMessages() == null
                ? ""
                : request.getMessages().stream()
                .map(ChatMessage::getContent)
                .collect(Collectors.joining("\n"));

        log.info("Local LLM stub responding: chars={}", prompt.length());

        if (prompt.contains("plotFacts") || prompt.contains("剧情事实")) {
            return """
                    {
                      "plotFacts": [
                        {"id": "pf_001", "summary": "本地开发环境已读取剧本内容，并生成可追溯的剧情理解占位结果。", "evidence": "dev-h2 local script"}
                      ],
                      "characterFacts": [],
                      "sceneFacts": [],
                      "uncertainties": [],
                      "coverageScore": 0.8
                    }
                    """;
        }

        if (prompt.contains("episode_number") || prompt.contains("生成每集")) {
            return """
                    [
                      {
                        "episode_number": 1,
                        "title": "本地推理第1集",
                        "summary": "主角发现关键线索，开始追查真相。",
                        "script": "主角在办公室翻阅资料，发现异常记录，决定继续调查。"
                      }
                    ]
                    """;
        }

        if (prompt.contains("sequence_num") || prompt.contains("storyboard")) {
            return """
                    [
                      {
                        "sequence_num": 1,
                        "scene_number": "001",
                        "short_description": "主角发现关键线索",
                        "detailed_description": "主角在办公室中查看文件，神情逐渐凝重，意识到事件并不简单。",
                        "characters": "",
                        "dialogues": "",
                        "camera_direction": "slow push in",
                        "composition_guide": "medium shot",
                        "duration_seconds": 5,
                        "start_frame_prompt": "cinematic office, tense mood, medium shot",
                        "motion_script": "镜头缓慢推进，主角低头查看文件后抬眼。"
                      }
                    ]
                    """;
        }

        if (prompt.contains("role_type") || prompt.contains("角色姓名")) {
            return """
                    [
                      {
                        "name": "主角",
                        "role_type": "female_lead",
                        "gender": "female",
                        "age": "25岁左右",
                        "personality": "冷静坚韧",
                        "costume": "浅色通勤装",
                        "visual_hint": "黑色长发，清冷气质",
                        "image_prompt": "young woman, black long hair, light office outfit, calm determined look"
                      }
                    ]
                    """;
        }

        if (prompt.contains("scene_type") || prompt.contains("场景名称")) {
            return """
                    [
                      {
                        "name": "办公室",
                        "scene_type": "indoor",
                        "description": "冷色调现代办公室，文件散落，气氛紧张",
                        "image_prompt": "wide establishing shot of modern office, cool grey palette, scattered documents, tense atmosphere"
                      }
                    ]
                    """;
        }

        if (prompt.contains("episode_count") || prompt.contains("结构化大纲")) {
            return """
                    {
                      "title": "本地推理短剧",
                      "genre": "现代都市",
                      "episode_count": 1,
                      "synopsis": "主角发现异常线索后展开调查，并逐步接近事件真相。"
                    }
                    """;
        }

        return "0-2s 主角发现线索；2-5s 镜头推进，气氛紧张，画面保持现代都市写实风格。";
    }

    @Override
    public String getModelId() {
        return "local-dev-llm";
    }

    @Override
    public String getModelName() {
        return "本地开发推理";
    }

    @Override
    public String getProvider() {
        return "local";
    }
}
