import { CreateBlogCommand } from '../../application/commands/CreateBlogCommand.js';
import { UpdateBlogCommand } from '../../application/commands/UpdateBlogCommand.js';
import { DeleteBlogCommand } from '../../application/commands/DeleteBlogCommand.js';
import { GetBlogQuery, GetAllBlogsQuery, GetUserBlogsQuery } from '../../application/queries/BlogQueries.js';

/**
 * BlogController
 * Blog műveletek kezelése
 */
export class BlogController {
  constructor(createHandler, updateHandler, deleteHandler, queryHandler) {
    this.createHandler = createHandler;
    this.updateHandler = updateHandler;
    this.deleteHandler = deleteHandler;
    this.queryHandler = queryHandler;
  }

  /**
   * Blog létrehozása
   * POST /api/blogs
   */
  async createBlog(req, res) {
    try {
      // FIGYELEM: req.user-t az auth middleware-nek kell beállítania
      // Ez a feladat része - implementálandó!
      const authorId = req.user?.id;
      
      if (!authorId) {
        return res.status(401).json({ error: 'Authentication required' });
      }

      const command = new CreateBlogCommand({
        ...req.body,
        authorId
      });

      const blog = await this.createHandler.handle(command);
      
      res.status(201).json({
        success: true,
        data: blog.toObject()
      });
    } catch (error) {
      res.status(400).json({
        success: false,
        error: error.message
      });
    }
  }

  /**
   * Blog módosítása
   * PUT /api/blogs/:id
   */
  async updateBlog(req, res) {
    try {
      const userId = req.user?.id;
      
      if (!userId) {
        return res.status(401).json({ error: 'Authentication required' });
      }

      const command = new UpdateBlogCommand(req.params.id, req.body);
      
      // FIGYELEM: Authorization ellenőrzés szükséges!
      // Csak a szerző vagy admin módosíthatja
      // Ez a feladat része - implementálandó!

      const blog = await this.updateHandler.handle(command);
      
      res.json({
        success: true,
        data: blog.toObject()
      });
    } catch (error) {
      res.status(400).json({
        success: false,
        error: error.message
      });
    }
  }

  /**
   * Blog törlése
   * DELETE /api/blogs/:id
   */
  async deleteBlog(req, res) {
    try {
      const userId = req.user?.id;
      
      if (!userId) {
        return res.status(401).json({ error: 'Authentication required' });
      }

      const command = new DeleteBlogCommand(req.params.id, userId);
      const result = await this.deleteHandler.handle(command);
      
      res.json({
        success: true,
        message: result.message
      });
    } catch (error) {
      res.status(400).json({
        success: false,
        error: error.message
      });
    }
  }

  /**
   * Egy blog lekérése
   * GET /api/blogs/:id
   */
  async getBlog(req, res) {
    try {
      const query = new GetBlogQuery(req.params.id);
      const blog = await this.queryHandler.handleGetBlog(query);
      
      res.json({
        success: true,
        data: blog.toObject()
      });
    } catch (error) {
      res.status(404).json({
        success: false,
        error: error.message
      });
    }
  }

  /**
   * Összes blog lekérése
   * GET /api/blogs
   */
  async getAllBlogs(req, res) {
    try {
      const query = new GetAllBlogsQuery({
        publishedOnly: req.query.published === 'true',
        limit: req.query.limit ? parseInt(req.query.limit) : undefined,
        offset: req.query.offset ? parseInt(req.query.offset) : 0
      });
      
      const blogs = await this.queryHandler.handleGetAllBlogs(query);
      
      res.json({
        success: true,
        data: blogs.map(blog => blog.toObject()),
        count: blogs.length
      });
    } catch (error) {
      res.status(400).json({
        success: false,
        error: error.message
      });
    }
  }

  /**
   * User blogjai
   * GET /api/users/:userId/blogs
   */
  async getUserBlogs(req, res) {
    try {
      const query = new GetUserBlogsQuery(req.params.userId);
      const blogs = await this.queryHandler.handleGetUserBlogs(query);
      
      res.json({
        success: true,
        data: blogs.map(blog => blog.toObject()),
        count: blogs.length
      });
    } catch (error) {
      res.status(400).json({
        success: false,
        error: error.message
      });
    }
  }
}
