using System;
using System.Collections.Generic;

namespace RedCircuit.AIAssistant
{
    // ==================== 消息模型 ====================

    public enum MessageRole { User, Assistant, System }

    [Serializable]
    public class ChatMessage
    {
        public MessageRole Role;
        public string Content;
        public DateTime Timestamp;
    }

    // ==================== 响应模型 ====================

    public enum ResponseType { Text, Analysis, Action, Error }

    [Serializable]
    public class AIResponse
    {
        public ResponseType Type;
        public string Content;
        public List<AIAction> Actions = new();
        public bool RequiresConfirmation;
        public CircuitAnalysisReport Analysis;
    }

    // ==================== 操作指令模型 ====================

    public enum ActionType { Place, Delete, Move, Rotate, Wire }

    [Serializable]
    public class AIAction
    {
        public ActionType Type;
        public string Component;       // 元件类型 (Place 时使用)
        public int X;                  // 目标 X 坐标
        public int Y;                  // 目标 Y 坐标
        public int FromX;              // 源 X 坐标 (Move/Wire 时使用)
        public int FromY;              // 源 Y 坐标 (Move/Wire 时使用)
        public int Rotation;           // 旋转角度 (0/90/180/270)
        public string Description;     // 操作描述 (供用户确认时显示)
    }

    // ==================== 上下文模型 ====================

    [Serializable]
    public class GameContext
    {
        public string Mode;            // creative / puzzle / automation / multiplayer
        public int CanvasWidth;
        public int CanvasHeight;
        public int ComponentCount;
        public string SelectedComponentId;
        public string SelectedComponentType;
        public int? LevelId;
        public string LevelTitle;
        public long RedstoneCoins;
    }

    // ==================== 电路分析模型 ====================

    [Serializable]
    public class CircuitAnalysisReport
    {
        public string Summary;
        public CircuitMetrics Metrics;
        public List<CircuitIssue> Issues = new();
        public List<CircuitOptimization> Optimizations = new();
    }

    [Serializable]
    public class CircuitMetrics
    {
        public int ComponentCount;
        public int TotalCost;
        public int MaxDelay;
        public int SignalPaths;
    }

    public enum IssueSeverity { Error, Warning, Info }

    [Serializable]
    public class CircuitIssue
    {
        public IssueSeverity Severity;
        public string Type;            // signal_loss / short_circuit / redundant / etc.
        public GridPosition Location;
        public string Description;
        public string Suggestion;
    }

    [Serializable]
    public class CircuitOptimization
    {
        public string Type;            // delay_reduction / cost_reduction / etc.
        public string Description;
        public string EstimatedImprovement;
    }

    // ==================== 电路方案模型 ====================

    [Serializable]
    public class CircuitPlan
    {
        public string Name;
        public string Description;
        public int Cost;
        public int Delay;
        public int ComponentCount;
        public List<AIAction> Steps = new();
    }

    // ==================== 辅助类型 ====================

    [Serializable]
    public struct GridPosition
    {
        public int X;
        public int Y;

        public GridPosition(int x, int y) { X = x; Y = y; }
    }

    /// <summary>操作批次 (用于撤销)</summary>
    public class ActionBatch
    {
        public List<AIAction> Actions = new();
        public string SnapshotBefore;
        public string SnapshotAfter;
    }
}
