using Contracts.BusinessLogicContracts;
using Contracts.SearchModels;
using Contracts.ViewModels;
using DatabaseImplement.Models;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;

namespace RestAPI.Controllers
{
    public class MediaFileController : Controller
    {
        private readonly IMediaFileLogic _mediaFileLogic;
        private readonly IProductLogic _productLogic;
        private readonly ILogger _logger;
        public MediaFileController(ILogger logger, IMediaFileLogic mediaFileLogic, IProductLogic productLogic)
        {
            _logger = logger;
            _mediaFileLogic = mediaFileLogic;
            _productLogic = productLogic;
        }
        [HttpGet]
        public List<MediaFileViewModel> GetFullList()
        {
            try
            {
                return _mediaFileLogic.ReadList(null);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения списка продуктов");
                throw;
            }
        }
        [HttpGet]
        public Dictionary<ProductViewModel, List<MediaFileViewModel>> GetByProducts()
        {
            try
            {
                var dict = new Dictionary<ProductViewModel, List<MediaFileViewModel>>();
                var products = _productLogic.ReadList(null);
                
                foreach (var product in products) 
                {
                    var media = _mediaFileLogic.ReadList(new MediaFileSearchModel()
                    {
                        ProductId = product.Id,
                    });
                    dict.Add(product, media);
                }
                return dict;
                
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка получения списка продуктов");
                throw;
            }
        }
    }
}
