package com.zioinfo.mall.auth;

import com.zioinfo.mall.common.ApiResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/mall/auth")
@RequiredArgsConstructor
public class AuthController {

    private final AuthService authService;

    @PostMapping("/login")
    public ApiResponse<Map<String, String>> login(@RequestBody LoginRequest req) {
        String token = authService.login(req.username(), req.password());
        return ApiResponse.ok(Map.of("token", token, "type", "Bearer"));
    }

    @PostMapping("/register")
    public ApiResponse<Map<String, String>> register(@RequestBody RegisterRequest req) {
        String token = authService.register(req.username(), req.password(), req.displayName());
        return ApiResponse.ok(Map.of("token", token, "type", "Bearer"));
    }

    @GetMapping("/me")
    public ApiResponse<Map<String, String>> me(@RequestHeader("Authorization") String header) {
        String token = header.replace("Bearer ", "");
        return ApiResponse.ok(authService.me(token));
    }

    record LoginRequest(String username, String password) {}
    record RegisterRequest(String username, String password, String displayName) {}
}
