using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using CounterDrone.Core.Models;

namespace CounterDrone.Core
{
    /// <summary>
    /// 统一默认数据 — 弹药、编队、火力单元、无人机、探测设备、天气的预设库。
    /// 所有默认数据集中在 data/defaults.json，通过本类统一加载。
    /// </summary>
    public class DefaultData
    {
        public string Version { get; set; } = "0";
        public List<AmmunitionSpec> Ammunition { get; set; } = new();
        public List<FormationTemplate> Formations { get; set; } = new();
        public List<RouteTemplate> Routes { get; set; } = new();
        [System.Text.Json.Serialization.JsonPropertyName("fireUnits")]
        public List<FireUnitSpec> FireUnits { get; set; } = new();
        [System.Text.Json.Serialization.JsonPropertyName("launchPlatforms")]
        public List<LaunchPlatformSpec> LaunchPlatforms { get; set; } = new();
        [System.Text.Json.Serialization.JsonPropertyName("drones")]
        public List<DroneSpec> Drones { get; set; } = new();
        [System.Text.Json.Serialization.JsonPropertyName("detectionEquipment")]
        public List<SensorSpec> Sensors { get; set; } = new();
        [System.Text.Json.Serialization.JsonPropertyName("weather")]
        public List<EnvironmentSpec> Environments { get; set; } = new();

        public static DefaultData Load(IPathProvider paths)
        {
            var jsonPath = Path.Combine(paths.GetDataRoot(), "defaults.json");
            if (!File.Exists(jsonPath))
                throw new FileNotFoundException($"默认数据文件未找到: {jsonPath}");

            var opts = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
            var data = JsonSerializer.Deserialize<DefaultData>(File.ReadAllText(jsonPath), opts)
                ?? throw new InvalidOperationException($"默认数据文件解析失败: {jsonPath}");

            Validate(data, jsonPath);
            return data;
        }

        public static DefaultData FromJson(string json)
        {
            var opts = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
            var data = JsonSerializer.Deserialize<DefaultData>(json, opts)
                ?? throw new InvalidOperationException("默认数据 JSON 解析失败");
            Validate(data, "<string>");
            return data;
        }

        private static void Validate(DefaultData data, string source)
        {
            if (data.Ammunition == null || data.Ammunition.Count == 0)
                throw new InvalidOperationException($"默认数据缺少 ammunition: {source}");
            if (data.Formations == null || data.Formations.Count == 0)
                throw new InvalidOperationException($"默认数据缺少 formations: {source}");
            if (data.Routes == null || data.Routes.Count == 0)
                throw new InvalidOperationException($"默认数据缺少 routes: {source}");
            if (data.FireUnits == null || data.FireUnits.Count == 0)
                throw new InvalidOperationException($"默认数据缺少 fireUnits: {source}");
            if (data.LaunchPlatforms == null || data.LaunchPlatforms.Count == 0)
                throw new InvalidOperationException($"默认数据缺少 launchPlatforms: {source}");
            if (data.Drones == null || data.Drones.Count == 0)
                throw new InvalidOperationException($"默认数据缺少 drones: {source}");
            if (data.Environments == null || data.Environments.Count == 0)
                throw new InvalidOperationException($"默认数据缺少 environments: {source}");
            if (data.Sensors == null)
                data.Sensors = new();
        }
    }
}
