using System;
using System.Collections.Generic;
using System.IO;
using CounterDrone.Core;
using CounterDrone.Core.Models;
using CounterDrone.Core.Services;
using CounterDrone.Core.Simulation;
using SQLite;
using Xunit;

namespace CounterDrone.Core.Tests
{
    public class ReportServiceTests : IDisposable
    {
        private readonly string _testDir;
        private readonly IReportService _service;
        private readonly SQLiteConnection _db;

        public ReportServiceTests()
        {
            _testDir = Path.Combine(Path.GetTempPath(), $"cd_rep_{Guid.NewGuid():N}");
            var paths = new TestPathProvider(_testDir);
            var dbm = new DatabaseManager(paths);
            _db = dbm.OpenMainDb();
            _service = new ReportService(_db, paths);
        }

        public void Dispose()
        {
            _db?.Close();
            if (Directory.Exists(_testDir)) Directory.Delete(_testDir, true);
        }

        [Fact]
        public void Generate_And_Get_ReturnsReport()
        {
            var config = new TaskFullConfig
            {
                Task = new SimTask { Name = "Test", TaskNumber = "SIM-001" },
                Targets = new List<TargetConfig> { new TargetConfig { Quantity = 1 } },
                Equipment = new List<EquipmentDeployment>(),
                Cloud = new CloudDispersal(),
            };

            var report = _service.Generate("task1", config, new List<SimEvent>(), "Destroyed", 45f);

            Assert.NotNull(report);
            Assert.NotEmpty(report.Id);
            Assert.Equal("Test", report.TaskName);
            Assert.Contains("仿真评估报告", report.Content);

            var fetched = _service.GetReport(report.Id);
            Assert.NotNull(fetched);
        }

        [Fact]
        public void Delete_RemovesReport()
        {
            var config = new TaskFullConfig
            {
                Task = new SimTask { Name = "Del", TaskNumber = "SIM-DEL" },
                Targets = new List<TargetConfig> { new TargetConfig { Quantity = 1 } },
                Equipment = new List<EquipmentDeployment>(),
                Cloud = new CloudDispersal(),
            };

            var report = _service.Generate("task1", config, new List<SimEvent>(), "Destroyed", 10f);
            _service.DeleteReport(report.Id);
            Assert.Null(_service.GetReport(report.Id));
        }

        [Fact]
        public void SearchReports_Keyword()
        {
            var cfg = new TaskFullConfig
            {
                Task = new SimTask { Name = "城市防御", TaskNumber = "SIM-A" },
                Targets = new List<TargetConfig> { new TargetConfig { Quantity = 1 } },
                Equipment = new List<EquipmentDeployment>(),
                Cloud = new CloudDispersal(),
            };

            _service.Generate("t1", cfg, new List<SimEvent>(), "Destroyed", 10f);
            cfg.Task.Name = "平原拦截";
            cfg.Task.TaskNumber = "SIM-B";
            _service.Generate("t2", cfg, new List<SimEvent>(), "Destroyed", 20f);

            var result = _service.SearchReports("城市", null, null, 1, 10);
            Assert.Equal(1, result.TotalCount);
        }
    }
}
