import { Router } from "express";
import { UserController } from "../controllers/UserController.js";
import { CreateUserRequestDto } from "../dtos/CreateUserRequestDto.js";
import { UserRepository } from "../repositories/UserRepository.js";
import { UserDtoEntityMapper } from "../mappers/UserDtoEntityMapper.js";
import { asyncHandler } from "../utils/asyncHandler.js";

/**
 * Handles all user related routes
 */
const router = Router();

// Inject repo + mapper here
const userRepository = new UserRepository();
const userMapper = new UserDtoEntityMapper();
const userController = new UserController(userRepository, userMapper);

/**
 * Create a new user
 */
router.post(
  "/",
  asyncHandler(async (req, res) => {
    const requestDto: CreateUserRequestDto = req.body;
    const responseDto = await userController.createUser(requestDto);
    res.status(201).json(responseDto);
  })
);

/**
 * Get user data for current user
 */
router.get("/me", 
  asyncHandler(async (req, res) => {
    
    const id = req.currentUser?.id;
    if(id){
      // it breaks here because id is no longer a uuid
      const responseDto = await userController.getUserById(id);
      res.status(201).json(responseDto);
    }
  })
);

export default router;
