const CreateUserCommand = require('../../application/user/command/CreateUserCommand');
const CreateUserHandler = require('../../application/user/command/CreateUserHandler');
const UpdateUserCommand = require('../../application/user/command/UpdateUserCommand');
const UpdateUserHandler = require('../../application/user/command/UpdateUserHandler');
const DeleteUserCommand = require('../../application/user/command/DeleteUserCommand');
const DeleteUserHandler = require('../../application/user/command/DeleteUserHandler');
const GetUserQuery = require('../../application/user/query/GetUserQuery');
const GetUserHandler = require('../../application/user/query/GetUserHandler');
const GetAllUsersQuery = require('../../application/user/query/GetAllUsersQuery');
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();
  }

  async getAllUsers(req, res) {
    try {
      const query = new GetAllUsersQuery();
      const users = await this.getAllUsersHandler.handle(query);
      
      res.json({
        success: true,
        data: users
      });
    } catch (error) {
      res.status(500).json({
        success: false,
        error: error.message
      });
    }
  }

  async getUserById(req, res) {
    try {
      const query = new GetUserQuery(parseInt(req.params.id));
      const user = await this.getUserHandler.handle(query);
      
      res.json({
        success: true,
        data: user
      });
    } catch (error) {
      res.status(404).json({
        success: false,
        error: error.message
      });
    }
  }

  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,
        data: user,
        message: 'User sikeresen létrehozva'
      });
    } catch (error) {
      res.status(400).json({
        success: false,
        error: error.message
      });
    }
  }

  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.json({
        success: true,
        data: user,
        message: 'User sikeresen frissítve'
      });
    } catch (error) {
      res.status(400).json({
        success: false,
        error: error.message
      });
    }
  }

  async deleteUser(req, res) {
    try {
      const command = new DeleteUserCommand(parseInt(req.params.id));
      const result = await this.deleteUserHandler.handle(command);
      
      res.json({
        success: true,
        message: result.message
      });
    } catch (error) {
      res.status(404).json({
        success: false,
        error: error.message
      });
    }
  }
}

module.exports = UserController;
