using Microsoft.Extensions.Logging;
using ProjectShoeShop.Repositories;

namespace ProjectShoeShop.Reports;

public class DocReport
{
    private readonly IClientRepository _clientRepository;
    private readonly IShoesRepository _shoesRepository;
    private readonly ILogger<DocReport> _logger;

    public DocReport(IClientRepository clientRepository, IShoesRepository shoesRepository, ILogger<DocReport> logger)
    {
        _clientRepository = clientRepository ?? throw new ArgumentNullException(nameof(clientRepository));
        _shoesRepository = shoesRepository ?? throw new ArgumentNullException(nameof(shoesRepository));
        _logger = logger ?? throw new ArgumentNullException(nameof(logger));
    }

    public bool CreateDoc(string filePath, bool includeClients, bool includeShoes)
    {
        try
        {
            var builder = new WordBuilder(filePath).AddHeader("Документ со справочниками");
            if (includeClients)
            {
                builder.AddParagraph("Клиенты").AddTable([2400], GetClients());
            }
            if (includeShoes)
            {
                builder.AddParagraph("Обувь").AddTable([2400, 2400, 2400], GetShoes());
            }
            builder.Build();
            return true;
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Ошибка при формировании документа");
            return false;
        }
    }

    private List<string[]> GetClients()
    {
        return [
            ["Имя"],
            .. _clientRepository
                .ReadClient()
                .Select(x => new string[] { x.Name }),
        ];
    }

    private List<string[]> GetShoes()
    {
        return [
            ["Название", "Цена", "Тип"],
            .. _shoesRepository
                .ReadShoes()
                .Select(x => new string[] { x.Name, x.Price.ToString(), x.ShoesType.ToString() }),
        ];
    }
}
