using DocuMed.Domain.Entities.MedicalHistoryTemplate;
using DocuMed.Repository.Repositories.Entities;
using DocuMed.Repository.Repositories.Entities.Abstracts;
using System.Threading;

namespace DocuMed.Core.EntityServices;

public class MedicalHistoryTemplateService : IMedicalHistoryTemplateService
{
    private readonly IRepositoryWrapper _repositoryWrapper;
    private readonly ICurrentUserService _currentUserService;
    private readonly IMedicalHistoryTemplateRepository _medicalHistoryTemplateRepository;

    public MedicalHistoryTemplateService(IRepositoryWrapper repositoryWrapper,ICurrentUserService currentUserService,IMedicalHistoryTemplateRepository medicalHistoryTemplateRepository)
    {
        _repositoryWrapper = repositoryWrapper;
        _currentUserService = currentUserService;
        _medicalHistoryTemplateRepository = medicalHistoryTemplateRepository;
    }
    public async Task<bool> EditAsync(MedicalHistoryTemplateLDto template, CancellationToken cancellationToken)
    {
        if (!Guid.TryParse(_currentUserService.UserId, out Guid userId))
            throw new AppException("توکن غیرمجاز", ApiResultStatusCode.UnAuthorized);
        if (template.Id==Guid.Empty)
            throw new AppException("پیش نویس پیدا نشد", ApiResultStatusCode.NotFound);
        var ent = MedicalHistoryTemplate.Create(template.ChiefComplaint, template.SectionId, userId);
        ent.Id = template.Id;

        var questions = await _repositoryWrapper.SetRepository<MedicalHistoryQuestion>()
            .TableNoTracking
            .Where(q => q.MedicalHistoryTemplateId == ent.Id)
            .ToListAsync(cancellationToken);
        foreach (var question in questions)
        {
            if (template.Questions.FirstOrDefault(q => q.Id == question.Id) == null)
            {
                _repositoryWrapper.SetRepository<MedicalHistoryQuestion>()
                    .Delete(question);
                await _repositoryWrapper.SaveChangesAsync(cancellationToken);
            }
        }

        foreach (var question in template.Questions.Where(q=>q.Id==Guid.Empty))
            ent.AddQuestion(question.Question, question.Part, question.QuestionType,question.BodySystem,question.IsSign,question.IsSymptom);
        _medicalHistoryTemplateRepository.Update(ent);
        await _repositoryWrapper.SaveChangesAsync(cancellationToken);
        return true;
    }

    public async Task<bool> AddAsync(MedicalHistoryTemplateLDto template,CancellationToken cancellationToken)
    {
        if (!Guid.TryParse(_currentUserService.UserId, out Guid userId))
            throw new AppException("توکن غیرمجاز", ApiResultStatusCode.UnAuthorized);
        var ent = MedicalHistoryTemplate.Create(template.ChiefComplaint, template.SectionId, userId);
        foreach (var question in template.Questions)
            ent.AddQuestion(question.Question, question.Part, question.QuestionType, question.BodySystem, question.IsSign, question.IsSymptom);
        _medicalHistoryTemplateRepository.Add(ent);
        await _repositoryWrapper.SaveChangesAsync(cancellationToken);
        return true;
    }
}