package com.codenetworkz.blog.mappers;

import com.codenetworkz.blog.dtos.PostDto;
import com.codenetworkz.blog.models.Post;
import com.codenetworkz.blog.models.Status;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import static com.codenetworkz.blog.mappers.DateConversion.convertToLocalDate;

@Component
public class PostMapper {

    private final TagMapper tagMapper;
    private final CategoryMapper categoryMapper;

    @Value("${service.file.url}")
     private String fileUrl ;

    PostMapper(TagMapper tagMapper, CategoryMapper categoryMapper){
        this.tagMapper = tagMapper;
        this.categoryMapper = categoryMapper;
    }


    public Optional<PostDto> postToPostDto(Optional<Post> post){
          PostDto postDto = new PostDto();
          if(post.isPresent()) {
              postDto.setPostId(post.get().getId());
              postDto.setName(post.get().getName());
              postDto.setBody(post.get().getContent());
              postDto.setSlug(post.get().getSlug());
              postDto.setFeaturedImage(fileUrl+post.get().getFeaturedImageUrl());
              if(post.get().getIsPublished()==null){
                  postDto.setPublishedStatus(Status.NO); // draft
              }else{
                  postDto.setPublishedStatus(post.get().getIsPublished());
              }
              postDto.setSelectedTags(tagMapper.tagListToTagDtoList(post.get().getTags()));
              postDto.setSelectedCategories(categoryMapper.categoryToCategaryDtoList(post.get().getCategories()));
              postDto.setCreatedAt(convertToLocalDate(post.get().getCreateAt()));
              postDto.setActiveStatus(post.get().getIsActive());

              postDto.setAdminStatus(post.get().getIsApproved());
              postDto.setAuther(post.get().getCreatedBy()!=null ? post.get().getCreatedBy() : "System");
          }
          return Optional.of(postDto);
        }

        public Optional<Post>postDtoToPost(PostDto postDto){
            Post post=new Post();
            if(postDto!=null) {
                post.setId(postDto.getPostId());
                post.setName(postDto.getName());
                post.setContent(postDto.getBody());
                post.setSlug(postDto.getSlug());
                post.setFeaturedImageUrl(fileUrl+postDto.getFeaturedImage());
                if (postDto.getPublishedStatus() == null) {
                    post.setIsPublished(Status.NO); // draft
                }else {
                    post.setIsPublished(postDto.getPublishedStatus()); // draft
                }
                 post.setTags(tagMapper.tagDtoListToTagList(postDto.getSelectedTags()));
               post.setCategories(categoryMapper.categoryListDtoToCategoryList(postDto.getSelectedCategories()));
                post.setIsActive(postDto.getActiveStatus());
                post.setIsApproved(postDto.getAdminStatus());
                post.setCreatedBy(postDto.getAuther());
            }
            return Optional.of(post);
        }

    public List<PostDto> postListToPostDtoList(List<Post> posts){
        List<PostDto> postDtoList =new ArrayList<>();
        if(!posts.isEmpty()) {
            postDtoList = posts.stream().map(post -> postToPostDto(Optional.ofNullable(post)).get()).toList();
        }
        return postDtoList;
    }

    public List<Post> postDtoListToPostList(List<PostDto> postDtoList){
        List<Post> postList =new ArrayList<>();
        if(!postDtoList.isEmpty()) {
            postList = postDtoList.stream().map(post -> postDtoToPost(post).get()).toList();
        }
        return postList;
    }
}
