package com.pharmacopoeia.controller;

import com.pharmacopoeia.dto.ApiResponse;
import com.pharmacopoeia.dto.LoginRequest;
import com.pharmacopoeia.security.JwtUtil;
import com.pharmacopoeia.service.AuthService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

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

    private final AuthService authService;
    private final JwtUtil jwtUtil;

    public AuthController(AuthService authService, JwtUtil jwtUtil) {
        this.authService = authService;
        this.jwtUtil = jwtUtil;
    }

    @PostMapping("/login/wechat")
    public ResponseEntity<Map<String, Object>> wechatLogin(@RequestBody LoginRequest request) {
        String openid = authService.wechatLogin(request.getCode());
        String token = jwtUtil.createToken(openid);
        return ResponseEntity.ok(Map.of(
                "access_token", token,
                "token_type", "bearer",
                "user_id", openid
        ));
    }
}
