package com.ankurm.hsc.snippets;

import com.ankurm.hsc.client.PostClient;
import com.ankurm.hsc.model.Post;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * The runner from the first version of the article, adapted to the module's PostClient.
 * Compiled against the module; not executed as a runner. The same five calls, with real output,
 * are made by Step01RegistryAndCrudTest against the local stub.
 */
@Configuration(proxyBeanMethods = false)
class DemoRunner {

    @Bean
    ApplicationRunner runner(PostClient postService) {
        return args -> {

            // GET all posts
            List<Post> allPosts = postService.findAll();
            System.out.println("Total posts: " + allPosts.size());

            // GET a single post
            Post post = postService.findById(1);
            System.out.println("Post 1 title: " + post.title());

            // POST a new post
            Post newPost = new Post(null, 1, "HTTP Service Clients", "Spring Boot 4 is amazing!");
            Post created = postService.create(newPost);
            System.out.println("Created post ID: " + created.id());

            // PUT update
            Post updated = postService.update(1, new Post(1, 1, "Updated Title", "Updated body"));
            System.out.println("Updated title: " + updated.title());

            // DELETE
            postService.delete(1);
            System.out.println("Deleted post 1");
        };
    }
}
