package top.tqx.demo_1.controller;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import top.tqx.demo_1.common.Result;
import top.tqx.demo_1.entity.User;
import top.tqx.demo_1.service.AuthService;

public class BaseController {

    @Autowired
    protected AuthService authService;

    protected Long getCurrentUserId(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        if (token == null || !token.startsWith("Bearer ")) {
            return null;
        }

        User user = authService.validateToken(token.substring(7));
        return user != null ? user.getId() : null;
    }

    protected <T> Result<T> success(T data) {
        return Result.success(data);
    }

    protected Result<Void> success() {
        return Result.success();
    }

    protected <T> Result<T> error(String message) {
        return Result.error(message);
    }

    protected <T> Result<T> error(Integer code, String message) {
        return Result.error(code, message);
    }
}
