package com.erp.controller;
import com.erp.dto.ApiResponse;
import com.erp.service.OpenApiProxyService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.*;

@RestController
@RequestMapping("/api/open-api-proxy")
@RequiredArgsConstructor
@Slf4j
public class OpenApiProxyController {
    private final OpenApiProxyService openApiProxyService;

    @GetMapping("/list")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getOpenApiProxyList(
            @RequestAttribute("company_code") String companyCode,
            @RequestParam Map<String, Object> params) {
        params.put("company_code", companyCode);
        return ResponseEntity.ok(ApiResponse.success(openApiProxyService.getOpenApiProxyList(params)));
    }

    @GetMapping("/{id}")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getOpenApiProxyInfo(
            @RequestAttribute("company_code") String companyCode,
            @PathVariable Long id) {
        Map<String, Object> params = new HashMap<>();
        params.put("company_code", companyCode);
        params.put("id", id);
        return ResponseEntity.ok(ApiResponse.success(openApiProxyService.getOpenApiProxyInfo(params)));
    }

    @PostMapping
    public ResponseEntity<ApiResponse<Integer>> insertOpenApiProxy(
            @RequestAttribute("company_code") String companyCode,
            @RequestBody Map<String, Object> body) {
        body.put("company_code", companyCode);
        return ResponseEntity.ok(ApiResponse.success(openApiProxyService.insertOpenApiProxy(body)));
    }

    @PutMapping("/{id}")
    public ResponseEntity<ApiResponse<Integer>> updateOpenApiProxy(
            @RequestAttribute("company_code") String companyCode,
            @PathVariable Long id,
            @RequestBody Map<String, Object> body) {
        body.put("company_code", companyCode);
        body.put("id", id);
        return ResponseEntity.ok(ApiResponse.success(openApiProxyService.updateOpenApiProxy(body)));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<ApiResponse<Integer>> deleteOpenApiProxy(
            @RequestAttribute("company_code") String companyCode,
            @PathVariable Long id) {
        Map<String, Object> params = new HashMap<>();
        params.put("company_code", companyCode);
        params.put("id", id);
        return ResponseEntity.ok(ApiResponse.success(openApiProxyService.deleteOpenApiProxy(params)));
    }
}
