package com.erp.controller;

import com.erp.dto.ApiResponse;
import com.erp.service.ShippingPlanService;
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/shipping-plan")
@RequiredArgsConstructor
@Slf4j
public class ShippingPlanController {

    private final ShippingPlanService shippingPlanService;

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

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

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

    @PostMapping("/batch")
    public ResponseEntity<ApiResponse<Map<String, Object>>> batchSave(
            @RequestAttribute("company_code") String companyCode,
            @RequestAttribute("user_id") String userId,
            @RequestBody Map<String, Object> body) {
        body.put("company_code", companyCode);
        body.put("user_id", userId);
        return ResponseEntity.ok(ApiResponse.success(shippingPlanService.batchSave(body)));
    }

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

    @PutMapping("/{id}")
    public ResponseEntity<ApiResponse<Map<String, Object>>> updateShippingPlan(
            @RequestAttribute("company_code") String companyCode,
            @RequestAttribute("user_id") String userId,
            @PathVariable Long id,
            @RequestBody Map<String, Object> body) {
        body.put("company_code", companyCode);
        body.put("updated_by", userId);
        body.put("id", id);
        return ResponseEntity.ok(ApiResponse.success(shippingPlanService.updateShippingPlan(body)));
    }

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