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

namespace RealEstateTransactions.Reports
{
    public class DocReport
    {
        private readonly IApartmentRepository _apartmentRepository;
        private readonly IBuyerRepository _buyerRepository;
        private readonly IServicesRepository _servicesRepository;
        private readonly ILogger<DocReport> _logger;

        public DocReport(IApartmentRepository apartmentRepository, IBuyerRepository buyerRepository,
        IServicesRepository servicesRepository, ILogger<DocReport> logger)
        {
            _apartmentRepository = apartmentRepository ?? throw new
                ArgumentNullException(nameof(apartmentRepository));
            _buyerRepository = buyerRepository ?? throw new 
                ArgumentNullException(nameof(buyerRepository));
            _servicesRepository = servicesRepository ?? throw new 
                ArgumentNullException(nameof(servicesRepository));
            _logger = logger ?? throw new ArgumentNullException(nameof(logger));
        }

        public bool CreateDoc(string filePath, bool includeApartments, bool
        includeBuyers, bool includeServices)
        {
            try
            {
                var builder = new WordBuilder(filePath)
                .AddHeader("Документ со справочниками");
                if (includeApartments)
                {
                    builder.AddParagraph("Квартиры")
                    .AddTable([1040, 1040, 1040, 1040, 1040, 1040],
                    GetApartments());
                }
                if (includeBuyers)
                {
                    builder.AddParagraph("Покупатели")
                    .AddTable([2400, 2400, 2400], GetBuyers());
                }
                if (includeServices)
                {
                    builder.AddParagraph("Услуги")
                    .AddTable([3600, 3600], GetServices());
                }
                builder.Build();

                return true;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка при формировании документа");
                return false;
            }
        }
        private List<string[]> GetApartments()
        {
            return [
                ["Номер агенства", "Комнаты", "Площадь", "Цена за кв. метр", "Базовая стоимость", "Желаемая цена продажи"],
                .. _apartmentRepository
                .ReadApartments()
                .Select(x => new string[] { x.Agency_ID.ToString(), x.Form_factor_ID.ToString(),
                x.Area.ToString(), x.Price_per_SM.ToString(), x.Base_price.ToString(), x.Desired_price.ToString() }),
                ];
        }
        private List<string[]> GetBuyers()
        {
            return [
                ["ФИО", "Серия паспорта", "Номер паспорта"],
                .. _buyerRepository
                .ReadBuyers()
                .Select(x => new string[] { x.Full_name, x.Passport_series.ToString(),
                x.Passport_number.ToString() }),
                ];
        }
        private List<string[]> GetServices()
        {
            return [
                ["Название", "Цена"],
                .. _servicesRepository
                .ReadServices()
                .Select(x => new string[] { x.Name, x.Price.ToString() }),
                ];
        }
    }
}