const GuestUserService = require('../../services/user/guest/GuestUserService');
const AbonemmentService = require('../../services/abonemments/AbonemmentService');
const { logger } = require('../../middleware/logger');

const abonemmentService = new AbonemmentService();

class GuestRegisterController {
  static async register(req, res) {
    logger.info('GuestRegisterController.register:start');
    try {
      const {
        firstName,
        lastName,
        email,
        confirmEmail,
        password,
        confirmPassword,
        referralEmail,
        lang,
      } = req.body;

      if (!email || !password || !firstName || !lastName) {
        return res.status(400).json({
          success: false,
          message: 'firstName, lastName, email, and password are required',
        });
      }

      if (email !== confirmEmail) {
        return res.status(400).json({
          success: false,
          message: 'Email and confirm email do not match',
        });
      }

      if (password !== confirmPassword) {
        return res.status(400).json({
          success: false,
          message: 'Password and confirm password do not match',
        });
      }

      const newUser = await GuestUserService.createGuestUser({
        email,
        password,
        firstName,
        lastName,
        referralEmail,
        lang,
      });

      logger.info('GuestRegisterController.register:success', { userId: newUser.id });

      res.status(201).json({
        success: true,
        message: 'Guest user registered successfully',
        userId: newUser.id,
      });
    } catch (error) {
      logger.error('GuestRegisterController.register:error', { error: error.message });

      if (error.message === 'User already exists') {
        return res.status(400).json({
          success: false,
          message: 'User already exists',
        });
      }

      res.status(500).json({
        success: false,
        message: 'Internal server error',
      });
    }
  }
}

module.exports = GuestRegisterController;
