package com.social.media.mapper;

import com.social.media.domain.entity.Post;
import com.social.media.dto.PostDto;
import org.springframework.stereotype.Component;

@Component
public class PostMapper {
    
    public PostDto toDto(Post post) {
        if (post == null) {
            return null;
        }
        
        PostDto dto = new PostDto();
        dto.setId(post.getId());
        dto.setPostCode(post.getPostCode());
        dto.setCompanyId(post.getCompanyId());
        dto.setAuthorId(post.getAuthorId());
        dto.setCategoryId(post.getCategoryId());
        dto.setContentText(post.getContentText());
        dto.setPostType(post.getPostType() != null ? post.getPostType().name() : null);
        dto.setStatus(post.getStatus() != null ? post.getStatus().name() : null);
        dto.setScheduledFor(post.getScheduledFor());
        dto.setPublishedAt(post.getPublishedAt());
        dto.setCreatedAt(post.getCreatedAt());
        dto.setUpdatedAt(post.getUpdatedAt());
        dto.setTags(post.getTags());
        dto.setIsPinned(post.getIsPinned());
        dto.setViewCount(post.getViewCount());
        dto.setLikeCount(post.getLikeCount());
        dto.setShareCount(post.getShareCount());
        dto.setCommentCount(post.getCommentCount());
        dto.setDeletedAt(post.getDeletedAt());
        
        // Set metrics for compatibility
        dto.setMetrics(createMetricsDto(post));
        
        return dto;
    }
    
    public Post toEntity(PostDto dto) {
        if (dto == null) {
            return null;
        }
        
        Post post = new Post();
        
        if (dto.getId() != null) {
            post.setId(dto.getId());
        }
        if (dto.getPostCode() != null) {
            post.setPostCode(dto.getPostCode());
        }
        
        post.setCompanyId(dto.getCompanyId());
        post.setAuthorId(dto.getAuthorId());
        post.setCategoryId(dto.getCategoryId());
        post.setContentText(dto.getContentText());
        
        if (dto.getPostType() != null) {
            try {
                post.setPostType(Post.PostType.valueOf(dto.getPostType().toUpperCase()));
            } catch (IllegalArgumentException e) {
                post.setPostType(Post.PostType.FEED); // Default
            }
        }
        
        if (dto.getStatus() != null) {
            try {
                post.setStatus(Post.PostStatus.valueOf(dto.getStatus().toUpperCase()));
            } catch (IllegalArgumentException e) {
                post.setStatus(Post.PostStatus.DRAFT); // Default
            }
        }
        
        post.setScheduledFor(dto.getScheduledFor());
        post.setPublishedAt(dto.getPublishedAt());
        post.setTags(dto.getTags());
        post.setIsPinned(dto.getIsPinned());
        post.setViewCount(dto.getViewCount());
        post.setLikeCount(dto.getLikeCount());
        post.setShareCount(dto.getShareCount());
        post.setCommentCount(dto.getCommentCount());
        post.setDeletedAt(dto.getDeletedAt());
        
        return post;
    }
    
    public void updateEntityFromDto(Post post, PostDto dto) {
        if (post == null || dto == null) {
            return;
        }
        
        if (dto.getContentText() != null) {
            post.setContentText(dto.getContentText());
        }
        if (dto.getPostType() != null) {
            try {
                post.setPostType(Post.PostType.valueOf(dto.getPostType().toUpperCase()));
            } catch (IllegalArgumentException e) {
                // Keep existing value
            }
        }
        if (dto.getStatus() != null) {
            try {
                post.setStatus(Post.PostStatus.valueOf(dto.getStatus().toUpperCase()));
            } catch (IllegalArgumentException e) {
                // Keep existing value
            }
        }
        if (dto.getScheduledFor() != null) {
            post.setScheduledFor(dto.getScheduledFor());
        }
        if (dto.getTags() != null) {
            post.setTags(dto.getTags());
        }
        if (dto.getIsPinned() != null) {
            post.setIsPinned(dto.getIsPinned());
        }
        if (dto.getCategoryId() != null) {
            post.setCategoryId(dto.getCategoryId());
        }
        if (dto.getViewCount() != null) {
            post.setViewCount(dto.getViewCount());
        }
        if (dto.getLikeCount() != null) {
            post.setLikeCount(dto.getLikeCount());
        }
        if (dto.getShareCount() != null) {
            post.setShareCount(dto.getShareCount());
        }
        if (dto.getCommentCount() != null) {
            post.setCommentCount(dto.getCommentCount());
        }
    }
    
    private PostDto.PostMetricsDto createMetricsDto(Post post) {
        if (post.getLikeCount() == null && post.getShareCount() == null && 
            post.getCommentCount() == null && post.getViewCount() == null) {
            return null;
        }
        
        PostDto.PostMetricsDto metrics = new PostDto.PostMetricsDto();
        metrics.setLikes(post.getLikeCount() != null ? post.getLikeCount() : 0);
        metrics.setShares(post.getShareCount() != null ? post.getShareCount() : 0);
        metrics.setComments(post.getCommentCount() != null ? post.getCommentCount() : 0);
        metrics.setViews(post.getViewCount() != null ? post.getViewCount() : 0);
        
        // Calculate engagement rate
        if (post.getViewCount() != null && post.getViewCount() > 0) {
            int totalEngagement = metrics.getLikes() + metrics.getShares() + metrics.getComments();
            double engagementRate = (double) totalEngagement / post.getViewCount() * 100;
            metrics.setEngagement(Math.round(engagementRate * 100.0) / 100.0); // Round to 2 decimal places
        } else {
            metrics.setEngagement(0.0);
        }
        
        return metrics;
    }
}
