package com.erp.controller;
import com.erp.dto.ApiResponse;
import com.erp.service.CollectionService;
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/collections")
@RequiredArgsConstructor
@Slf4j
public class CollectionController {
    private final CollectionService collectionService;

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

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

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

    @GetMapping("/{id}")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getCollectionInfo(
            @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(collectionService.getCollectionInfo(params)));
    }

    @GetMapping("/{configId}/history")
    public ResponseEntity<ApiResponse<List<Map<String, Object>>>> getCollectionHistory(
            @RequestAttribute("company_code") String companyCode,
            @PathVariable Long configId) {
        Map<String, Object> params = new HashMap<>();
        params.put("company_code", companyCode);
        params.put("config_id", configId);
        return ResponseEntity.ok(ApiResponse.success(collectionService.getCollectionHistory(params)));
    }

    @PostMapping
    public ResponseEntity<ApiResponse<Map<String, Object>>> insertCollection(
            @RequestAttribute("company_code") String companyCode,
            @RequestBody Map<String, Object> body) {
        body.put("company_code", companyCode);
        return ResponseEntity.status(201).body(ApiResponse.success(collectionService.insertCollection(body), "수집 설정이 생성되었습니다."));
    }

    @PostMapping("/{id}/execute")
    public ResponseEntity<ApiResponse<Map<String, Object>>> executeCollection(
            @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(collectionService.executeCollection(params)));
    }

    @PutMapping("/{id}")
    public ResponseEntity<ApiResponse<Map<String, Object>>> updateCollection(
            @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(collectionService.updateCollection(body)));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<ApiResponse<Map<String, Object>>> deleteCollection(
            @RequestAttribute("company_code") String companyCode,
            @PathVariable Long id) {
        Map<String, Object> params = new HashMap<>();
        params.put("company_code", companyCode);
        params.put("id", id);
        collectionService.deleteCollection(params);
        return ResponseEntity.ok(ApiResponse.success(null, "수집 설정이 삭제되었습니다."));
    }
}
