package main

import (
	"fmt"
	"os"
)

func main() {
	if len(os.Args) > 1 && os.Args[1] == "vision-stream" {
		if err := runVisionStream(os.Args[2:]); err != nil {
			logError(err.Error())
			os.Exit(1)
		}
		return
	}

	if len(os.Args) > 1 && os.Args[1] == "ax-probe" {
		if err := runAXProbe(); err != nil {
			logError(err.Error())
			os.Exit(1)
		}
		return
	}

	if len(os.Args) > 1 && os.Args[1] == "sync-chat" {
		if err := runChatSync(); err != nil {
			logError(err.Error())
			os.Exit(1)
		}
		return
	}

	if err := runOnce(); err != nil {
		logError(err.Error())
		os.Exit(1)
	}
}

func runOnce() error {
	logInfo("Go RPA agent demo 启动")

	config, err := loadConfig("config.toml")
	if err != nil {
		return fmt.Errorf("读取 config.toml 失败：%w", err)
	}
	logInfo("已读取 config.toml，模型=" + config.Volcengine.Model)

	regionsPath := appDataPath(config, config.Agent.RegionsPath)
	screenshotPath := appDataPath(config, config.Agent.ScreenshotPath)
	annotation, err := loadAnnotation(regionsPath)
	if err != nil {
		return fmt.Errorf("读取标注文件失败：%w", err)
	}
	summaries := summarizeRegions(annotation.Regions)
	summaries, inferenceWarnings := inferMissingRegionTypes(summaries)
	logInfo(fmt.Sprintf("已读取标注文件，regions=%d", len(summaries)))
	for _, warning := range inferenceWarnings {
		logWarning(warning)
	}
	if err := validateRequiredRegions(summaries); err != nil {
		return err
	}

	for _, summary := range summaries {
		logThink(fmt.Sprintf("区域识别：%s center=[%.1f, %.1f]", summary.Type, summary.CenterScreen[0], summary.CenterScreen[1]))
	}

	if err := maybeScrollBeforeObserve(config, summaries, screenshotPath); err != nil {
		return fmt.Errorf("滚动获取更多聊天记录失败：%w", err)
	}

	imageDataURL, err := loadImageDataURL(screenshotPath)
	if err != nil {
		return fmt.Errorf("读取截图失败：%w", err)
	}
	logInfo("已读取截图 WeChat.jpg")

	prompt, err := buildPrompt(summaries)
	if err != nil {
		return fmt.Errorf("构造 prompt 失败：%w", err)
	}

	logThink("正在请求豆包多模态模型识别聊天内容并生成任务计划")
	rawResponse, err := requestLLM(config, prompt, imageDataURL)
	if err != nil {
		return fmt.Errorf("请求 LLM 失败：%w", err)
	}

	task, _, err := parseTaskFromLLM(rawResponse)
	if err != nil {
		_ = saveRawResponse(config, rawResponse)
		return fmt.Errorf("解析 LLM task JSON 失败，已保存 latest_task_raw.txt：%w", err)
	}

	task = finalizeTask(task, summaries, config, regionsPath, screenshotPath)
	logJSON("summary", fmt.Sprintf(
		"LLM 摘要：联系人=%s；最新消息=%s；回复=%s；confidence=%.2f",
		task.LLMSummary.ContactName,
		task.LLMSummary.LatestUserMessage,
		task.LLMSummary.ReplyText,
		task.Confidence,
	))
	logTask(fmt.Sprintf("已生成任务 %s，type=%s，actions=%d，dry_run=%v", task.TaskID, task.TaskType, len(task.Actions), config.Agent.DryRun))

	for _, action := range task.Actions {
		logTask(fmt.Sprintf("动作记录：%s %s status=%s target=%s", action.ActionID, action.Name, action.Status, action.TargetRegion))
	}

	latestPath, historyPath, err := saveTaskFiles(task, config)
	if err != nil {
		return fmt.Errorf("保存 task 文件失败：%w", err)
	}
	logInfo("已保存 latest task: " + latestPath)
	logInfo("已保存 history task: " + historyPath)
	logInfo("Go RPA agent demo 完成，未真实执行鼠标键盘")

	return nil
}
