using PdfMarker.Models;

namespace PdfMarker.Services;

public class BallooningService
{
    private readonly PdfQuotaExtractor _quotaExtractor;
    private readonly QuotaClusterer _clusterer;
    private readonly IWebHostEnvironment _env;

    public BallooningService(
        PdfQuotaExtractor quotaExtractor,
        QuotaClusterer clusterer,
        IWebHostEnvironment env)
    {
        _quotaExtractor = quotaExtractor;
        _clusterer = clusterer;
        _env = env;
    }

    public async Task<AutoBalloonResult> GenerateAsync(string fileName)
    {
        // 1️⃣ Path fisico del PDF
        var pdfPath = Path.Combine(
            _env.WebRootPath,
            "pdf",
            fileName);

        // 2️⃣ Estrazione QUOTE
        var quotes = _quotaExtractor.Extract(pdfPath);

        // 🔴 CHECK CRITICO
        // se qui quotes è vuoto → il problema NON è il clustering
        if (quotes.Count == 0)
        {
            return new AutoBalloonResult
            {
                Balloons = new List<BalloonVm>()
            };
        }

        // 3️⃣ Clustering QUOTE → FEATURE
        var features = _clusterer.Cluster(quotes);

        // 4️⃣ Feature → Pallini
        var balloons = new List<BalloonVm>();
        int index = 1;

        foreach (var feature in features)
        {
            balloons.Add(new BalloonVm
            {
                Number = index++,
                Description = string.Join(" | ",
                    feature.Quotes.Select(q => q.RawText)),
                X = feature.CenterX,
                Y = feature.CenterY,
                Selected = false
            });
        }

        // 5️⃣ Output finale
        return new AutoBalloonResult
        {
            Balloons = balloons
        };
    }
}