package com.social.media.interfaces.web.mapper;

import java.util.Set;
import java.util.stream.Collectors;

import org.springframework.stereotype.Component;

import com.social.media.application.content.command.CreatePostCommand;
import com.social.media.application.content.command.UpdatePostCommand;
import com.social.media.domain.company.valueobject.CompanyId;
import com.social.media.domain.content.aggregate.Post;
import com.social.media.domain.content.valueobject.PostId;
import com.social.media.domain.content.valueobject.PostType;
import com.social.media.interfaces.web.dto.content.ContentResponse;
import com.social.media.interfaces.web.dto.content.CreateContentRequest;
import com.social.media.interfaces.web.dto.content.UpdateContentRequest;

/**
 * Mapper for Content/Post DTOs
 */
@Component
public class ContentDtoMapper {

    /**
     * Convert CreateContentRequest to CreatePostCommand
     */
    public CreatePostCommand toCreateCommand(CreateContentRequest request) {
        return new CreatePostCommand(
                new CompanyId(request.companyId()),
                null, // authorId - will be set by controller based on authentication
                null, // categoryId - not provided in request
                request.title() != null ? request.title() : "Content",
                request.text(),
                PostType.valueOf(request.contentType().toUpperCase()),
                request.mediaUrls(),
                request.socialAccountIds() != null ? 
                    request.socialAccountIds().stream()
                        .map(id -> String.valueOf(id))
                        .collect(Collectors.toSet()) : Set.of(),
                request.scheduledDateTime(),
                request.hashtags() != null ? String.join(" ", request.hashtags()) : null,
                false // isPinned
        );
    }

    /**
     * Convert UpdateContentRequest to UpdatePostCommand
     */
    public UpdatePostCommand toUpdateCommand(PostId postId, UpdateContentRequest request) {
        return new UpdatePostCommand(
                postId,
                request.title() != null ? request.title() : "Content",
                request.text(),
                null, // categoryId - not provided in request
                request.mediaUrls(),
                request.socialAccountIds() != null ? 
                    request.socialAccountIds().stream()
                        .map(id -> String.valueOf(id))
                        .collect(Collectors.toSet()) : Set.of(),
                request.scheduledDateTime(),
                request.hashtags() != null ? String.join(" ", request.hashtags()) : null,
                false // isPinned
        );
    }

    /**
     * Convert Post aggregate to ContentResponse
     */
    public ContentResponse toResponse(Post post) {
        return new ContentResponse(
                post.getId().value(),
                post.getCompanyId().value(),
                post.getContent().text(),
                "", // title - not available in domain
                "", // description - not available in domain
                post.getMediaIds().stream()
                    .map(mediaId -> String.valueOf(mediaId.value()))
                    .collect(Collectors.toList()),
                post.getHashtags(),
                post.getPostType().name(),
                post.getStatus().name(),
                post.getScheduledFor(),
                post.getPublishedAt(),
                post.getTargetSocialAccounts().stream()
                    .map(account -> account.value())
                    .collect(Collectors.toList()),
                post.getScheduledFor() != null,
                post.getCreatedAt(),
                post.getUpdatedAt()
        );
    }
}
