using Demo.Core.Enums;
using Demo.Data;
using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using Microsoft.AspNetCore.Mvc;
using System.Linq;

namespace Demo.Web.Controllers
{
    [Produces("application/json")]
    [Route("api/[controller]")]
    [ApiController]
    public class UsersController : ControllerBase
    {
        private readonly IPtItemsRepository _itemsRepository;
        private readonly IPtUserRepository _userRepository;

        public UsersController(IPtItemsRepository itemsRepository, IPtUserRepository userRepository)
        {
            _itemsRepository = itemsRepository;
            _userRepository = userRepository;
        }

        [HttpGet]
        [HttpPost]
        public DataSourceResult GetUsers([DataSourceRequest] DataSourceRequest request)
        {
            var users = _userRepository.GetAll().ToList();
            return users.ToDataSourceResult(request);
        }

        [HttpGet]
        [HttpPost]
        [Route("GetItemsByUser")]
        public DataSourceResult GetItemsByUser([DataSourceRequest] DataSourceRequest request, int userId)
        {
            var items = _itemsRepository.GetUserItems(userId).Where(item => item.Status != StatusEnum.Closed);
            return items.ToDataSourceResult(request);
        }
    }
}
