using System;
using System.Collections.Generic;
using UnityEngine;

namespace RedCircuit.AIAssistant
{
    /// <summary>
    /// 聊天 UI 控制器，处理消息收发与流式显示。
    /// </summary>
    public class AIChatController : MonoBehaviour
    {
        /// <summary>聊天面板是否可见</summary>
        public bool IsVisible { get; private set; }

        /// <summary>消息列表 (用于 UI 渲染)</summary>
        public List<ChatMessage> Messages { get; } = new();

        /// <summary>显示聊天面板</summary>
        public void Show()
        {
            IsVisible = true;
            // TODO: 播放面板展开动画
        }

        /// <summary>隐藏聊天面板</summary>
        public void Hide()
        {
            IsVisible = false;
            // TODO: 播放面板收起动画
        }

        /// <summary>追加用户消息到 UI</summary>
        public void AppendUserMessage(string content)
        {
            var msg = new ChatMessage
            {
                Role = MessageRole.User,
                Content = content,
                Timestamp = DateTime.Now
            };
            Messages.Add(msg);
            // TODO: 实例化用户消息气泡 UI
        }

        /// <summary>追加 AI 消息到 UI (支持流式追加)</summary>
        public void AppendAIMessage(string content)
        {
            var msg = new ChatMessage
            {
                Role = MessageRole.Assistant,
                Content = content,
                Timestamp = DateTime.Now
            };
            Messages.Add(msg);
            // TODO: 实例化 AI 消息气泡 UI，支持打字机效果
        }

        /// <summary>追加系统消息</summary>
        public void AppendSystemMessage(string content)
        {
            var msg = new ChatMessage
            {
                Role = MessageRole.System,
                Content = content,
                Timestamp = DateTime.Now
            };
            Messages.Add(msg);
            // TODO: 实例化系统消息 (居中灰色样式)
        }

        /// <summary>追加错误消息</summary>
        public void AppendErrorMessage(string content)
        {
            AppendSystemMessage($"[错误] {content}");
        }

        /// <summary>显示操作确认提示</summary>
        public void ShowConfirmationPrompt(int actionCount)
        {
            // TODO: 显示 "AI 建议执行 N 个操作，是否确认？" 确认栏
            Debug.Log($"[AIChat] 等待用户确认 {actionCount} 个操作");
        }

        /// <summary>清空聊天记录</summary>
        public void ClearHistory()
        {
            Messages.Clear();
            // TODO: 清空 UI 消息列表
        }
    }
}
