using System.Collections.Generic;
using CounterDrone.Core.Models;

namespace CounterDrone.Core.Algorithms
{
    /// <summary>火力单元（完整的武器系统）</summary>
    public class FireUnit
    {
        public string Id { get; set; } = string.Empty;
        public PlatformType Type { get; set; }
        public Vector3 Position { get; set; }

        // ── 发射装置 ──
        /// <summary>火炮/发射器数量</summary>
        public int GunCount { get; set; } = 1;
        /// <summary>每门炮的火力通道数（同时可装填的弹药数量）</summary>
        public int ChannelsPerGun { get; set; } = 1;
        /// <summary>总火力通道数 = GunCount × ChannelsPerGun</summary>
        public int TotalChannels => GunCount * ChannelsPerGun;
        /// <summary>同一通道连续发射的最小间隔（秒）</summary>
        public float ChannelInterval { get; set; } = 1f;

        // ── 弹药 ──
        /// <summary>总载弹量</summary>
        public int TotalMunitions { get; set; }
        /// <summary>可装填的弹药类型</summary>
        public List<AerosolType> AmmoTypes { get; set; } = new();
        /// <summary>单通道射击后冷却时间（秒）</summary>
        public float Cooldown { get; set; } = 5f;
        /// <summary>更换弹种时间（秒）</summary>
        public float AmmoChangeTime { get; set; } = 30f;

        // ── 搜索跟踪 ──
        /// <summary>雷达探测距离（m），0 表示无此设备</summary>
        public float RadarRange { get; set; }
        /// <summary>光电探测距离（m）</summary>
        public float EORange { get; set; }
        /// <summary>红外探测距离（m）</summary>
        public float IRRange { get; set; }

        // ── 空基 ──
        public float CruiseSpeed { get; set; }
        public float ReleaseAltitude { get; set; }

        // ── 地基 ──
        public float MuzzleVelocity { get; set; } = 800f;
    }

    /// <summary>无人机编队（规划器输入）</summary>
    public class DroneGroup
    {
        public string GroupId { get; set; } = string.Empty;
        public TargetConfig Target { get; set; } = new();
        public RoutePlan Route { get; set; } = new();
        public List<Waypoint> Waypoints { get; set; } = new();

        /// <summary>到达防御区域中点的时间（秒），由规划器预计算</summary>
        public float ArrivalTime { get; set; }
        /// <summary>威胁指数（速度系数 × 类型系数）</summary>
        public float ThreatIndex { get; set; }
        /// <summary>综合优先级 = 威胁指数 / (到达时间 + 1)</summary>
        public float Priority => ArrivalTime > -1 ? ThreatIndex / (ArrivalTime + 1f) : ThreatIndex;

        /// <summary>预计到达航路中点的时间（秒）。
        /// 物理含义：匀速直线运动从航路起点到中点的飞行时间。</summary>
        public float GetArrivalTime()
        {
            if (Waypoints.Count < 2) return 0;
            var s = Waypoints[0];
            var e = Waypoints[^1];
            float midX = ((float)s.PosX + (float)e.PosX) / 2f;
            float midZ = ((float)s.PosZ + (float)e.PosZ) / 2f;
            return Kinematics.TravelTime((float)s.PosX, (float)s.PosZ, midX, midZ, (float)Target.TypicalSpeed);
        }
    }

    /// <summary>三维向量（纯 C#，不依赖 UnityEngine）</summary>
    public struct Vector3
    {
        public float X, Y, Z;
        public Vector3(float x, float y, float z) { X = x; Y = y; Z = z; }
    }

    /// <summary>粒子渲染参数</summary>
    public class ParticleParams
    {
        public float Opacity { get; set; } = 1.0f;
        public float SizeMultiplier { get; set; } = 1.0f;
    }

    /// <summary>发射计划中的单次事件（算法生成 → 引擎执行）</summary>
    public class FireEvent
    {
        public float FireTime;
        public int PlatformIndex;
        public float TargetX, TargetY, TargetZ;
        public float MuzzleVelocity;
    }

    // ═══════════════════════════════════════════════
    // DefensePlanner 类型
    // ═══════════════════════════════════════════════

    /// <summary>拦截候选：一个火力单元对一个威胁的可行性评估</summary>
    public class InterceptCandidate
    {
        public FireUnit Unit { get; set; } = null!;
        public AerosolType AmmoType { get; set; }
        public float EarliestInterceptTime { get; set; }
        public float KillProbability { get; set; }
    }

    /// <summary>单元分配：一个火力单元被分配给一个威胁的配置</summary>
    public class UnitAssignment
    {
        public string FireUnitId { get; set; } = string.Empty;
        public string DroneGroupId { get; set; } = string.Empty;
        public AerosolType AmmoType { get; set; }
        public int RoundsFired { get; set; }
        public float FirstFireTime { get; set; }
        public List<FireEvent> FireEvents { get; set; } = new();
    }

    /// <summary>规划方案</summary>
    public class DefensePlan
    {
        public List<UnitAssignment> Assignments { get; set; } = new();
        public List<FireEvent> MergedSchedule { get; set; } = new();
        public float OverallProbability { get; set; }
        public int ThreatsEngaged { get; set; }
        public int ThreatsUnengaged { get; set; }
        public string Summary { get; set; } = string.Empty;
    }

    /// <summary>规划器输出</summary>
    public class PlannerResult
    {
        public DefensePlan Best { get; set; } = new();
        public DefensePlan Critical { get; set; } = new();
    }
}
