# /controllers/user_controller.py
from flask import jsonify, request, current_app
from app.services import UserService
from app.schemas import RegisterSchema
from pydantic import ValidationError
from app.schemas import ResponseSchema
from app.exception import AlreadyExistException
from app.helpers import make_response


class UserController:
    def __init__(self, userService: UserService):
        self.user_service = userService

    def register(self):
        try:
            request_data = request.get_json()
            register_data = RegisterSchema(**request_data)
            self.user_service.register_user(register_data)
            return make_response("Register Success")

        except ValidationError as e:
            current_app.logger.error(f"Validation error: {e}")
            response = ResponseSchema(message="Invalid input", data=None, meta=None)
            return make_response("Invalid input", status_code=400)

        except AlreadyExistException as e:
            return make_response("User already exists", status_code=409)
        except Exception as e:
            current_app.logger.error(
                f"Error during Google login: {str(e)}", exc_info=True
            )
            return make_response("Internal server error", status_code=500)
