package com.social.media.application.content.handler;

import com.social.media.application.content.command.CancelPostCommand;
import com.social.media.domain.content.aggregate.Post;
import com.social.media.domain.content.repository.PostRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class CancelPostCommandHandler {
    
    private final PostRepository postRepository;
    
    public CancelPostCommandHandler(PostRepository postRepository) {
        this.postRepository = postRepository;
    }
    
    public void handle(CancelPostCommand command) {
        // Find existing post
        Post post = postRepository.findById(command.postId())
            .orElseThrow(() -> new RuntimeException(
                "Post not found: " + command.postId()
            ));
        
        // Cancel the post by scheduling it for null (returns to DRAFT)
        post.scheduleFor(null);
        
        // Save cancelled post
        postRepository.save(post);
    }
}

