using AutoMapper;
using Entities.Contracts;
using Entities.DTO;
using Entities.DTO.Class;

namespace TechHelper.Context
{
	public class AutoMapperProFile : Profile
	{

		public static class EnumMappingHelpers
		{
			public static TEnum ParseEnumSafe<TEnum>(string sourceString, TEnum defaultValue) where TEnum : struct, Enum
			{
				if (Enum.TryParse(sourceString, true, out TEnum parsedValue))
				{
					return parsedValue;
				}
				return defaultValue;
			}
		}

		public AutoMapperProFile()
		{
			CreateMap<UserForRegistrationDto, User>()
				.ForMember(dest => dest.Id, opt => opt.Ignore())
				.ForMember(dest => dest.UserName, opt => opt.MapFrom(src => src.DisplayName))
				.ForMember(dest => dest.Email, opt => opt.MapFrom(src => src.Email))
				.ForMember(dest => dest.PhoneNumber, opt => opt.MapFrom(src => src.PhoneNumber))
				.ForMember(dest => dest.HomeAddress, opt => opt.MapFrom(src => src.HomeAddress))
				.ForMember(dest => dest.PasswordHash, opt => opt.Ignore())
				.ForMember(dest => dest.EmailConfirmed, opt => opt.Ignore());

			// Subject
			CreateMap<Subject, SubjectDto>().ReverseMap();
			CreateMap<Subject, SubjectResponseDto>().ReverseMap();
			CreateMap<Subject, TypeCommonDto>().ReverseMap();

			// ExamType
			CreateMap<ExamType, ExamTypeDto>().ReverseMap();
			CreateMap<ExamType, TypeCommonDto>().ReverseMap();

			// QuestionType
			CreateMap<QuestionType, QuestionTypeDto	>().ReverseMap();
			CreateMap<QuestionType, TypeCommonDto>().ReverseMap();


			// Exam 
			CreateMap<ExamDto, Exam>().ReverseMap();
			CreateMap<Exam, ExamListDto>().ReverseMap();

			CreateMap<ExamQuestionDto, ExamQuestion>().ReverseMap();

			CreateMap<QuestionDto, Question>().ReverseMap();

			// Submission
			CreateMap<SubmissionDto, Submission>().ReverseMap();

			CreateMap<SubmissionDetailDto, SubmissionDetail>().ReverseMap();
			CreateMap<SubmissionTeacherUpdateDto, Submission>().ReverseMap();

			// Student Submission Detail
			CreateMap<Submission, StudentSubmissionDetailDto>()
				.ForMember(dest => dest.AssignmentId, opt => opt.MapFrom(src => src.ExamId))
				.ForMember(dest => dest.StudentId, opt => opt.MapFrom(src => src.StudentId))
				.ForMember(dest => dest.SubmissionTime, opt => opt.MapFrom(src => src.SubmissionTime))
				.ForMember(dest => dest.OverallGrade, opt => opt.MapFrom(src => src.OverallGrade))
				.ForMember(dest => dest.OverallFeedback, opt => opt.MapFrom(src => src.OverallFeedback))
				.ForMember(dest => dest.Status, opt => opt.MapFrom(src => src.Status));

			CreateMap<Submission, SubmissionListDto>().ReverseMap();
			CreateMap<SubmissionDetail, SubmissionDetailTeacherUpdateDto>().ReverseMap();


			// School
			CreateMap<SchoolDto, School>().ReverseMap();
			CreateMap<SchoolResponseDto, School>().ReverseMap();

			// Grade
			CreateMap<GradeDto, Grade>().ReverseMap();
			CreateMap<CreateGradeDto, Grade>().ReverseMap();


			// Class
			CreateMap<ClassDto, Class>().ReverseMap();
			CreateMap<ClassCreateDto, Class>().ReverseMap();


			// User
			CreateMap<User, UserDto>().ReverseMap();
			CreateMap<User, UserListDto>().ReverseMap();

			// KeyPoint
			CreateMap<KeyPoint, KeyPointDto>().ReverseMap();
			CreateMap<KeyPoint, CreateKeyPointDto>().ReverseMap();
			CreateMap<KeyPoint, UpdateKeyPointDto>().ReverseMap();
			CreateMap<KeyPoint, KeyPointResponseDto>().ReverseMap();

			// Lesson
			CreateMap<Lesson, LessonDto>().ReverseMap();
			CreateMap<Lesson, CreateLessonDto>().ReverseMap();
			CreateMap<Lesson, UpdateLessonDto>().ReverseMap();
			CreateMap<Lesson, LessonResponseDto>().ReverseMap();

			// Textbook
			CreateMap<Textbook, TextbookDto>().ReverseMap();
			CreateMap<Textbook, CreateTextbookDto>().ReverseMap();
			CreateMap<Textbook, UpdateTextbookDto>().ReverseMap();
			CreateMap<Textbook, TextbookResponseDto>()
				.ForMember(dest => dest.LessonCount, opt => opt.MapFrom(src => src.Lessons.Count))
				.ForMember(dest => dest.LessonTitles, opt => opt.MapFrom(src => src.Lessons.Select(l => l.Title).ToList()));
		}
	}

}
