import { PostRepository } from '../../Infrastructure/postRepository.js';
import { GetAllPostsQuery } from '../../Application/blogs/query/getAllPostsQuery.js';
import { GetAllPostsQueryHandler } from '../../Application/blogs/query/getAllPostsQueryHandler.js';
import { GetPostByIdQuery } from '../../Application/blogs/query/getPostByIdQuery.js';
import { GetPostByIdQueryHandler } from '../../Application/blogs/query/getPostByIdQueryHandler.js';
import { GetPostsByUserIdQuery } from '../../Application/blogs/query/getPostsByUserIdQuery.js';
import { GetPostsByUserIdQueryHandler } from '../../Application/blogs/query/getPostsByUserIdQueryHandler.js';
import { CreatePostCommand } from '../../Application/blogs/command/createPostCommand.js';
import { CreatePostCommandHandler } from '../../Application/blogs/command/createPostCommandHandler.js';
import { UpdatePostCommand } from '../../Application/blogs/command/updatePostCommand.js';
import { UpdatePostCommandHandler } from '../../Application/blogs/command/updatePostCommandHandler.js';
import { DeletePostCommand } from '../../Application/blogs/command/deletePostCommand.js';
import { DeletePostCommandHandler } from '../../Application/blogs/command/deletePostCommandHandler.js';

const postRepository = new PostRepository();
const getAllPostsQueryHandler = new GetAllPostsQueryHandler(postRepository);
const getPostByIdQueryHandler = new GetPostByIdQueryHandler(postRepository);
const getPostsByUserIdQueryHandler = new GetPostsByUserIdQueryHandler(postRepository);
const createPostCommandHandler = new CreatePostCommandHandler(postRepository);
const updatePostCommandHandler = new UpdatePostCommandHandler(postRepository);
const deletePostCommandHandler = new DeletePostCommandHandler(postRepository);

export class PostController {
  async getAll(req, res) {
    try {
      const query = new GetAllPostsQuery();
      const posts = await getAllPostsQueryHandler.handle(query);
      res.json(posts);
    } catch (error) {
      res.status(500).json({ error: error.message });
    }
  }

  async getById(req, res) {
    try {
      const query = new GetPostByIdQuery(req.params.id);
      const post = await getPostByIdQueryHandler.handle(query);
      res.json(post);
    } catch (error) {
      res.status(404).json({ error: error.message });
    }
  }

  async getByUserId(req, res) {
    try {
      const query = new GetPostsByUserIdQuery(req.params.userId);
      const posts = await getPostsByUserIdQueryHandler.handle(query);
      res.json(posts);
    } catch (error) {
      res.status(500).json({ error: error.message });
    }
  }

  async create(req, res) {
    try {
      const command = new CreatePostCommand(
        req.body.title,
        req.body.content,
        req.body.author
      );
      const post = await createPostCommandHandler.handle(command);
      res.status(201).json(post);
    } catch (error) {
      res.status(400).json({ error: error.message });
    }
  }

  async update(req, res) {
    try {
      const command = new UpdatePostCommand(
        req.params.id,
        req.body.title,
        req.body.content,
        req.body.author
      );
      const post = await updatePostCommandHandler.handle(command);
      res.json(post);
    } catch (error) {
      res.status(404).json({ error: error.message });
    }
  }

  async delete(req, res) {
    try {
      const command = new DeletePostCommand(req.params.id);
      await deletePostCommandHandler.handle(command);
      res.status(204).send();
    } catch (error) {
      res.status(404).json({ error: error.message });
    }
  }
}
