/**
 * UpdateBlogHandler
 * UpdateBlogCommand handler
 */
export class UpdateBlogHandler {
  constructor(blogRepository) {
    this.blogRepository = blogRepository;
  }

  async handle(command) {
    const validation = command.validate();
    if (!validation.isValid) {
      throw new Error(`Validation failed: ${validation.errors.join(', ')}`);
    }

    // Ellenőrizzük, hogy létezik-e a blog
    const existingBlog = await this.blogRepository.findById(command.id);
    if (!existingBlog) {
      throw new Error('Blog not found');
    }

    const updateData = {};
    if (command.title !== undefined) updateData.title = command.title;
    if (command.content !== undefined) updateData.content = command.content;
    if (command.published !== undefined) updateData.published = command.published;

    const blog = await this.blogRepository.update(command.id, updateData);
    return blog;
  }
}
