using Microsoft.Extensions.Logging;
using RealEstateTransactions.Repositories;

namespace RealEstateTransactions.Reports
{
    public class ChartReport
    {
        private readonly IDealRepository _dealRepository;

        private readonly ILogger<ChartReport> _logger;

        public ChartReport(IDealRepository dealRepository, ILogger<ChartReport> logger)
        {
            _dealRepository = dealRepository ?? throw new
            ArgumentNullException(nameof(dealRepository));
            _logger = logger ?? throw new ArgumentNullException(nameof(logger));
        }

        public bool CreateChart(string filePath, DateTime dateTime)
        {
            try
            {
                new PdfBuilder(filePath)
                .AddHeader("Доход с продажи квартир")
                .AddPieChart("Проданные квартиры", GetData(dateTime))
                .Build();
                return true;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка при формировании документа");
                return false;
            }
        }

        private List<(string Caption, double Value)> GetData(DateTime dateTime)
        {
            return _dealRepository
            .ReadDeals()
            .Where(x => x.Deal_date.Date == dateTime.Date)
            .Select(x => (x.Apartment_ID.ToString(), (double)x.Deal_price))
            .ToList();
        }
    }
}