import { Router } from "express";
import { AuthController } from "../controllers/AuthController.js";
import { UserRepository } from "../repositories/UserRepository.js";
import { UserDtoEntityMapper } from "../mappers/UserDtoEntityMapper.js";
import {
  ValidationError,
  UnauthorizedError,
  InternalServerError,
} from "../errors/httpErrors.js";
import { LoginRequestDto } from "../dtos/LoginRequestDto.js";

export const  authBasicRoute = "/auth"

const router = Router();
const userRepository = new UserRepository();
const mapper = new UserDtoEntityMapper();
const authController = new AuthController(userRepository, mapper);

router.post("/login", async (req, res) => {
    console.log("login point called")
  try {
    const requestDto: LoginRequestDto = req.body;
    const responseDto = await authController.login(requestDto);
    res.json(responseDto);
  } catch (err: any) {
    if (err instanceof ValidationError || err instanceof UnauthorizedError) {
      res.status(err.statusCode).json({ error: err.message });
    } else {
      console.error("Unexpected error:", err);
      const internalError = new InternalServerError("Unexpected error");
      res.status(internalError.statusCode).json({ error: internalError.message });
    }
  }
});

export default router;
