using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using GxPress.Auth;
using GxPress.Common.Tools;
using GxPress.Entity;
using GxPress.Repository.Interface;
using GxPress.Repository.Interface.AppReport;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace GxPress.Api.AppControllers
{
    /// <summary>
    /// App数据报表
    /// </summary>
    [Route("api/app/app-report")]
    [ApiController]
    [Authorize]

    public class AppReportController : ControllerBase
    {
        private readonly IAppReportRepository _appReportRepository;
        private readonly IUserRepository userRepository;
        private readonly ILoginContext _loginContext;

        public AppReportController(IAppReportRepository appReportRepository, IUserRepository userRepository, ILoginContext _loginContext)
        {
            _appReportRepository = appReportRepository;
            this.userRepository = userRepository;
            this._loginContext = _loginContext;
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <returns></returns>
        [HttpPost("list")]
        public async Task<IEnumerable<Entity.AppReport>> GetAppReportList()
        {
            var user = await userRepository.GetAsync(_loginContext.AccountId);
            if (!user.IsReport)
                return new List<Entity.AppReport>();
            var result = await _appReportRepository.GetAppReportListAsync();
            var appReportList = result as AppReport[] ?? result.ToArray();
            foreach (var appReport in appReportList)
            {
                appReport.IocUrl = StringUtils.AddDomain(appReport.IocUrl);
            }

            return appReportList;
        }
    }
}