namespace MyOffice.Web.Models.Account
{
	using System.ComponentModel.DataAnnotations;
	using AutoMapper;
	using Core.Extensions;
	using Data.Models.Accounts;
	using Services.Account.Domain;
	using Services.Identity;
	using User;

	public class AccountViewModel
	{
		public string? Id { get; set; }

		[Required]
		public string Name { get; set; } = null!;
		public string Type { get; set; } = null!;

		[Required]
		public string CurrencyId { get; set; } = null!;
		public string? CurrencyName { get; set; }
		public bool AllowDelete { get; set; }

		public List<AccountCategoryViewModel>? Categories { get; set; }

		[Required]
		public string CategoryId { get; set; } = null!;

		public List<AccessRightsViewModel>? AccessRights { get; set; }
	}

	public class ViewModelProfile : Profile
	{
		public ViewModelProfile()
		{
			CreateMap<Guid, string>().ConvertUsing(x => x.ToShort());
		}
	}

	public class AccountViewModelProfile : Profile
	{
		public AccountViewModelProfile(
			IContextProvider contextProvider
		)
		{

			CreateMap<AccountDetailedDto, AccountDetailedViewModel>()
				.ForMember(x => x.Account, o => o.MapFrom(x => x.Account))
				;

			CreateMap<UserDto, UserViewModel>();
			
			CreateMap<MyOffice.Data.Models.Users.User, UserViewModel>();

			CreateMap<AccountDto, AccountViewModel>()
				.ForMember(x => x.Type, o => o.MapFrom(x => x.AccessRights!.FirstOrDefault(a => a.UserId == contextProvider.UserId)!.Type.ToString()))
				.ForMember(x => x.CurrencyId, o => o.MapFrom(x => x.CurrencyGlobalId))
				.ForMember(x => x.CurrencyName, o => o.MapFrom(x => x.Currency!.Name))
				.ForMember(x => x.AllowDelete, o => o.MapFrom(x => !x.HasMotions))
				;

			CreateMap<AccountAccountCategoryDto, AccountCategoryViewModel>()
				.ForMember(x => x.Id, o => o.MapFrom(x => x.CategoryId))
				.ForMember(x => x.Name, o => o.MapFrom(x => x.Category!.Name))
				;
			
			CreateMap<AccountAccessDto, AccessRightsViewModel>();
		}
	}

	/*public static class AccountViewModelExtensions
	{
		public static AccountViewModel ToModel(this AccountDto input)
		{
			return new AccountViewModel
			{
				Id = input.Id.ToShort(),
				Name = input.Name,
				CurrencyId = input.CurrencyGlobalId,
				CurrencyName = input.CurrencyGlobal!.Name,
				Categories = input.Categories!
					.Select(x => x.Category!.ToModel())
					.ToList(),
				AccessRights = input.AccessRights!
					.Select(x => x.ToModel())
					.ToList(),
			};
		}
	}*/
}
