using AutoMapper;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tsi1.BusinessLayer.Dtos;
using Tsi1.BusinessLayer.Helpers;
using Tsi1.BusinessLayer.Interfaces;
using Tsi1.DataLayer;
using Tsi1.DataLayer.Entities;

namespace Tsi1.BusinessLayer.Services
{
    public class UserService : IUserService
    {
        private readonly Tsi1Context _context;

        private readonly IMapper _mapper;

        public UserService(Tsi1Context context, IMapper mapper)
        {
            _context = context;
            _mapper = mapper;
        }

        public async Task<ServiceResult<User>> Authenticate(string username, string password, int tenantId)
        {
            var result = new ServiceResult<User>();

            var user = await _context.Users
                .Include(x => x.UserType)
                .FirstOrDefaultAsync(x => x.Username == username && x.TenantId == tenantId);

            if (user == null)
            {
                result.HasError = true;
                result.Message = string.Format(ErrorMessages.UserDoesNotExist, username);
                return result;
            }

            if (user.Password != password)
            {
                result.HasError = true;
                result.Message = ErrorMessages.IncorrectPassword;
                return result;
            }

            result.Data = user;

            return result;
        }

        public async Task<ServiceResult<User>> Create(UserRegisterDto dto, string type, int tenantId)
        {
            var result = new ServiceResult<User>();

            var user = _mapper.Map<User>(dto);
            user.TenantId = tenantId;
    
            if (type == UserTypes.Student)
            {
                user.Student = new Student()
                {
                    IdentityCard = dto.IdentityCard,
                    Age = dto.Age,
                    TenantId = tenantId
                };
            }

            if (type == UserTypes.Professor)
            {
                user.Professor = new Professor()
                {
                    IdentityCard = dto.IdentityCard,
                    TenantId = tenantId
                };
            }

            _context.Users.Add(user);
            await _context.SaveChangesAsync();
            result.Data = user;

            return result;
        }

        public async Task<ServiceResult<List<UserPreviewDto>>> GetAll(int tenantId)
        {
            var result = new ServiceResult<List<UserPreviewDto>>();

            var users = await _context.Users
                .Where(x => x.UserType.Name != UserTypes.FacultyAdmin && x.TenantId == tenantId)
                .ToListAsync();

            var usersDto = _mapper.Map<List<UserPreviewDto>>(users);

            result.Data = usersDto;

            return result;
        }

        public async Task<ServiceResult<UserPreviewDto>> GetById(int userId)
        {
            var result = new ServiceResult<UserPreviewDto>();

            var user = await _context.Users
                .Include(x => x.UserType)
                .Include(x => x.Student)
                .Include(x => x.Professor)
                .FirstOrDefaultAsync(x => x.Id == userId);

            if (user == null)
            {
                result.HasError = true;
                result.Message = string.Format(ErrorMessages.UserDoesNotExist, userId);
                return result;
            }

            var userType = user.UserType.Name;

            if (userType == UserTypes.Student && user.Student == null)
            {
                result.HasError = true;
                result.Message = string.Format(ErrorMessages.StudentDoesNotExist, userId);
                return result;                
            }
            else if(userType == UserTypes.Professor && user.Professor == null)
            {
                result.HasError = true;
                result.Message = string.Format(ErrorMessages.ProffesorDoesNotExist, userId);
                return result;
            }

            var userDto = _mapper.Map<UserPreviewDto>(user);

            result.Data = userDto;

            return result;
        }
    }
}