using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Entities.DTO;

namespace Entities.Contracts
{
	[Table("assignments")]
	public class Assignment
	{
		[Key]
		[Column("id")]
		public Guid Id { get; set; }

		[Required]
		[Column("title")]
		[StringLength(255)]
		public string Title { get; set; }

		[Column("description")]
		public string Description { get; set; }

		[Column("subject_area")]
		public SubjectAreaEnum SubjectArea { get; set; }

		[Required]
		[Column("exam_struct_id")]
		public Guid ExamStructId { get; set; }

		[Required]
		[Column("due_date")]
		public DateTime DueDate { get; set; }

		[Column("total_points")]
		public byte TotalQuestions { get; set; }

		[Column("score")]
		public float Score { get; set; }

		public string Name { get; set; } = string.Empty;

		public ExamType ExamType { get; set; } = ExamType.DailyTest;

		[Column("created_by")]
		public Guid CreatorId { get; set; }

		[Column("created_at")]
		public DateTime CreatedAt { get; set; }

		[Column("updated_at")]
		public DateTime UpdatedAt { get; set; } = DateTime.UtcNow;

		[Column("deleted")]
		public bool IsDeleted { get; set; } = false;

		// Navigation Properties

		[ForeignKey(nameof(CreatorId))]
		public User Creator { get; set; }
		public ICollection<AssignmentClass> AssignmentClasses { get; set; }

		[ForeignKey(nameof(ExamStructId))]
		public AssignmentQuestion ExamStruct { get; set; }
		public ICollection<AssignmentAttachment> AssignmentAttachments { get; set; }
		public ICollection<Submission> Submissions { get; set; }

		public Assignment()
		{
			Id = Guid.NewGuid();

			Submissions = new HashSet<Submission>();
			AssignmentClasses = new HashSet<AssignmentClass>();
			AssignmentAttachments = new HashSet<AssignmentAttachment>();
		}
	}


	public static class AssignmentExt
	{
		public static Submission ConvertToSubmission(this Assignment assignment, Guid studentId, Guid GraderId)
		{
			if (assignment == null) return new Submission();
			var submission = new Submission();

			submission.StudentId = studentId;
			submission.SubmissionTime = DateTime.Now;
			submission.Status = SubmissionStatus.Pending;
			submission.GraderId = GraderId;
			submission.AssignmentId = assignment.Id;

			ConvertExamSturctToSubmissionDetails(assignment.ExamStruct, studentId, submission.SubmissionDetails);

			return submission;
		}


		public static void ConvertExamSturctToSubmissionDetails(AssignmentQuestion examStruct, Guid studentId, ICollection<SubmissionDetail> submissions)
		{
			if (examStruct == null) return;
			submissions.Add(new SubmissionDetail
			{
				StudentId = studentId,
				AssignmentQuestionId = examStruct.Id,
				IsCorrect = true,
				CreatedAt = DateTime.Now,
				UpdatedAt = DateTime.Now,
				Status = SubmissionStatus.Pending,
			});

			examStruct.ChildrenAssignmentQuestion?.ToList().ForEach(s =>
			{
				ConvertExamSturctToSubmissionDetails(s, studentId, submissions);
			});
		}
	}
}
