package ru.mail.park.database.kgulyy.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import ru.mail.park.database.kgulyy.controllers.exceptions.PostNotFoundException;
import ru.mail.park.database.kgulyy.controllers.exceptions.ThreadNotFoundException;
import ru.mail.park.database.kgulyy.domains.*;
import ru.mail.park.database.kgulyy.domains.Thread;
import ru.mail.park.database.kgulyy.services.ForumService;
import ru.mail.park.database.kgulyy.services.PostService;
import ru.mail.park.database.kgulyy.services.ThreadService;
import ru.mail.park.database.kgulyy.services.UserService;

import java.util.List;
import java.util.Optional;

/**
 * @author Konstantin Gulyy
 */
@RestController
@RequestMapping("/api/post/{id}/details")
public class PostController {
    private final UserService userService;
    private final ForumService forumService;
    private final ThreadService threadService;
    private final PostService postService;

    @Autowired
    PostController(UserService userService, ForumService forumService,
                   ThreadService threadService, PostService postService) {
        this.userService = userService;
        this.forumService = forumService;
        this.threadService = threadService;
        this.postService = postService;
    }

    @GetMapping
    ResponseEntity<PostFull> getPostDetails(
            @PathVariable long id,
            @RequestParam(required = false) List<String> related
    ) {
        final Post post = postService.findById(id)
                .orElseThrow(() -> PostNotFoundException.throwEx(String.valueOf(id)));

        final PostFull postFull = new PostFull(post);
        if (related != null) {
            if (related.contains("user")) {
                final Optional<User> foundAuthor = userService.findByNickname(post.getAuthor());
                if (foundAuthor.isPresent()) {
                    final User author = foundAuthor.get();
                    postFull.setAuthor(author);
                }
            }
            if (related.contains("thread")) {
                final Optional<Thread> foundThread = threadService.findById(post.getThread());
                if (foundThread.isPresent()) {
                    final Thread thread = foundThread.get();
                    postFull.setThread(thread);
                }
            }
            if (related.contains("forum")) {
                final Optional<Forum> foundForum = forumService.findBySlug(post.getForum());
                if (foundForum.isPresent()) {
                    final Forum forum = foundForum.get();
                    postFull.setForum(forum);
                }
            }
        }

        return ResponseEntity.ok(postFull);
    }

    @PostMapping
    ResponseEntity<Post> updatePost(@PathVariable long id, @RequestBody Post updatedPost) {
        final Post post = postService.findById(id)
                .orElseThrow(() -> ThreadNotFoundException.throwEx(id));

        final String updatedMesage = updatedPost.getMessage();
        post.setMessage(updatedMesage);
        post.setEdited(true);

        postService.update(post);

        return ResponseEntity.ok(post);
    }
}

@@ -3020,22 +3020,20 @@
 w(() -%3E 
-Thread
+Post
 NotFound
@@ -3050,20 +3050,36 @@
 throwEx(
+String.valueOf(
 id))
+)
 ;%0A%0A     
@@ -3132,24 +3132,176 @@
 tMessage();%0A
+        if (updatedMesage == null) %7B%0A            return ResponseEntity.ok(post);%0A        %7D%0A%0A        if (!updatedMesage.equals(post.getMessage())) %7B%0A    
         post
@@ -3328,16 +3328,20 @@
 esage);%0A
+    
         
@@ -3362,17 +3362,20 @@
 (true);%0A
-%0A
+    
         
@@ -3399,16 +3399,26 @@
 e(post);
+%0A        %7D
 %0A%0A      

Fix some bugs in updating post