
using Kimuser.Domain;
using Kimuser.Domain.SystemUsers;
using Kimuser.Dto;
using Kimuser.Dto.Items.Users;
using Kimuser.Infrastructure.Repositories;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Kimuser.Services
{
    public interface IUserService
    {
        Task<User> GetUserAsync(string keyword);
        Task<List<SelectOptionUserItem>> SearchUserAsync(string keyword);
        Task<List<SelectOptionUserItem>> GetUsersAsync(string ids);
        //Task<ProfileModel> GetUserProfileAsync(int userId);
    }
    public class UserService : IUserService
    {
        private readonly IUserRepository<User> _userRepository;
        private readonly IUserRepository<SystemUser> _systemUserRepository;
        public UserService(IUserRepository<User> userRepository, IUserRepository<SystemUser> systemUserRepository)
        {
            _userRepository = userRepository;
            _systemUserRepository = systemUserRepository;
        }
        public async Task<User> GetUserAsync(string keyword)
        {
            var user = await _userRepository.Query().FirstOrDefaultAsync(x => x.UserName == keyword || x.Email == keyword || x.PhoneNumber == keyword);
            return user;
        }

        public async Task<List<SelectOptionUserItem>> SearchUserAsync(string keyword)
        {
            var query = _systemUserRepository.Query();
            var users = new List<SystemUser>();
            if (!string.IsNullOrEmpty(keyword))
            {
                keyword = keyword.Trim().ToLower();
                users = await query.Where(x => x.UserName.Contains(keyword) || x.Email.Contains(keyword) || x.PhoneNumber.Contains(keyword)).Take(10).ToListAsync();
            }
            else
            {
                users = await query.Take(10).ToListAsync();
            }
            
            return users.Select(u=>u.ToSelectOptionUserItem()).ToList();
        }

        public async Task<List<SelectOptionUserItem>> GetUsersAsync(string ids)
        {
            var query = _systemUserRepository.Query();
            var users = new List<SystemUser>();
            if (!string.IsNullOrEmpty(ids))
            {
                var listIds = ids.Split(',').Select(o=>int.Parse(o)).ToList();
                users = await query.Where(x => listIds.Contains(x.Id)).Take(100).ToListAsync();
            }
            else
            {
                users = await query.Take(10).ToListAsync();
            }

            return users.Select(u => u.ToSelectOptionUserItem()).ToList();
        }



        //public async Task<ProfileModel> GetUserProfileAsync(int userId)
        //{
        //    var profile = new ProfileModel();
        //    var user = await _userRepository.Query().FirstOrDefaultAsync(x => x.Id == userId);
        //    if (user != null && user.Id > 0)
        //    {
        //        profile.UserName = user.UserName;
        //        profile.Email = user.Email;

        //    }
        //    return profile;
        //}
    }
}
