const CreateUserCommand = require('../../application/user/command/CreateUserCommand');
const UpdateUserCommand = require('../../application/user/command/UpdateUserCommand');
const DeleteUserCommand = require('../../application/user/command/DeleteUserCommand');
const GetUserQuery = require('../../application/user/query/GetUserQuery');
const GetAllUsersQuery = require('../../application/user/query/GetAllUsersQuery');

const CreateUserHandler = require('../../application/user/command/CreateUserHandler');
const UpdateUserHandler = require('../../application/user/command/UpdateUserHandler');
const DeleteUserHandler = require('../../application/user/command/DeleteUserHandler');
const GetUserHandler = require('../../application/user/query/GetUserHandler');
const GetAllUsersHandler = require('../../application/user/query/GetAllUsersHandler');

class UserController {
  constructor() {
    this.createUserHandler = new CreateUserHandler();
    this.updateUserHandler = new UpdateUserHandler();
    this.deleteUserHandler = new DeleteUserHandler();
    this.getUserHandler = new GetUserHandler();
    this.getAllUsersHandler = new GetAllUsersHandler();
  }

  // GET /api/users - Összes user lekérése
  async getAllUsers(req, res) {
    try {
      const query = new GetAllUsersQuery();
      const users = await this.getAllUsersHandler.handle(query);
      
      res.status(200).json({
        success: true,
        data: users
      });
    } catch (error) {
      res.status(500).json({
        success: false,
        message: error.message
      });
    }
  }

  // GET /api/users/:id - Egy user lekérése ID alapján
  async getUserById(req, res) {
    try {
      const query = new GetUserQuery(parseInt(req.params.id));
      const user = await this.getUserHandler.handle(query);
      
      res.status(200).json({
        success: true,
        data: user
      });
    } catch (error) {
      res.status(404).json({
        success: false,
        message: error.message
      });
    }
  }

  // POST /api/users - Új user létrehozása
  async createUser(req, res) {
    try {
      const { email, name, password, role } = req.body;
      const command = new CreateUserCommand(email, name, password, role);
      const user = await this.createUserHandler.handle(command);
      
      res.status(201).json({
        success: true,
        message: 'User sikeresen létrehozva!',
        data: user
      });
    } catch (error) {
      res.status(400).json({
        success: false,
        message: error.message
      });
    }
  }

  // PUT /api/users/:id - User frissítése
  async updateUser(req, res) {
    try {
      const { email, name, role } = req.body;
      const command = new UpdateUserCommand(
        parseInt(req.params.id),
        email,
        name,
        role
      );
      const user = await this.updateUserHandler.handle(command);
      
      res.status(200).json({
        success: true,
        message: 'User sikeresen frissítve!',
        data: user
      });
    } catch (error) {
      res.status(400).json({
        success: false,
        message: error.message
      });
    }
  }

  // DELETE /api/users/:id - User törlése
  async deleteUser(req, res) {
    try {
      const command = new DeleteUserCommand(parseInt(req.params.id));
      const result = await this.deleteUserHandler.handle(command);
      
      res.status(200).json({
        success: true,
        message: result.message
      });
    } catch (error) {
      res.status(404).json({
        success: false,
        message: error.message
      });
    }
  }
}

module.exports = UserController;
