const GetMeQuery = require('../../application/user/queries/GetMeQuery');
const GetAllUsersQuery = require('../../application/user/queries/GetAllUsersQuery');
const GetUserByIdQuery = require('../../application/user/queries/GetUserByIdQuery');
const UpdateUserProfileCommand = require('../../application/user/commands/UpdateUserProfileCommand');

/**
 * User Controller
 * User-related endpoints using CQRS pattern (protected by JWT)
 */
class UserController {
  constructor(getMeQueryHandler, getAllUsersQueryHandler, getUserByIdQueryHandler, updateUserProfileCommandHandler) {
    this.getMeQueryHandler = getMeQueryHandler;
    this.getAllUsersQueryHandler = getAllUsersQueryHandler;
    this.getUserByIdQueryHandler = getUserByIdQueryHandler;
    this.updateUserProfileCommandHandler = updateUserProfileCommandHandler;
  }

  /**
   * GET /api/users/me - Bejelentkezett user adatai (protected)
   */
  async getMe(req, res) {
    try {
      // req.user-t az authMiddleware tölti ki a JWT-ből
      const userId = req.user.userId;

      const query = new GetMeQuery(userId);
      const user = await this.getMeQueryHandler.handle(query);

      res.status(200).json({
        message: 'User retrieved successfully',
        data: user
      });
    } catch (error) {
      const status = error.message.includes('not found') ? 404 : 500;
      res.status(status).json({ error: error.message });
    }
  }

  /**
   * GET /api/users - Összes user lekérése (protected)
   */
  async getAll(req, res) {
    try {
      const query = new GetAllUsersQuery();
      const users = await this.getAllUsersQueryHandler.handle(query);

      res.status(200).json({
        message: 'Users retrieved successfully',
        data: users,
        count: users.length
      });
    } catch (error) {
      res.status(500).json({ error: error.message });
    }
  }

  /**
   * GET /api/users/:id - User lekérése ID alapján (protected)
   */
  async getById(req, res) {
    try {
      const { id } = req.params;
      const userId = parseInt(id);

      if (isNaN(userId)) {
        return res.status(400).json({ error: 'Invalid user ID' });
      }

      const query = new GetUserByIdQuery(userId);
      const user = await this.getUserByIdQueryHandler.handle(query);

      res.status(200).json({
        message: 'User retrieved successfully',
        data: user
      });
    } catch (error) {
      const status = error.message.includes('not found') ? 404 : 500;
      res.status(status).json({ error: error.message });
    }
  }

  /**
   * PUT /api/users/me - User profil frissítése (protected)
   */
  async updateMe(req, res) {
    try {
      const userId = req.user.userId;
      const { name } = req.body;

      const command = new UpdateUserProfileCommand(userId, name);
      const user = await this.updateUserProfileCommandHandler.handle(command);

      res.status(200).json({
        message: 'Profile updated successfully',
        data: user
      });
    } catch (error) {
      const status = error.message.includes('required') ? 400 : 500;
      res.status(status).json({ error: error.message });
    }
  }
}

module.exports = UserController;
