package com.erp.controller;

import com.erp.dto.ApiResponse;
import com.erp.service.ShippingOrderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/shipping-order")
@RequiredArgsConstructor
@Slf4j
public class ShippingOrderController {

    private final ShippingOrderService shippingOrderService;

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

    @GetMapping("/preview-no")
    public ResponseEntity<ApiResponse<Map<String, Object>>> previewNextNo(
            @RequestAttribute("company_code") String companyCode) {
        Map<String, Object> params = new HashMap<>();
        params.put("company_code", companyCode);
        return ResponseEntity.ok(ApiResponse.success(shippingOrderService.previewNextNo(params)));
    }

    @PostMapping("/save")
    public ResponseEntity<ApiResponse<Map<String, Object>>> save(
            @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(shippingOrderService.save(body)));
    }

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

    @GetMapping("/source/shipment-plan")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getShipmentPlanSource(
            @RequestAttribute("company_code") String companyCode,
            @RequestParam Map<String, Object> params) {
        params.put("company_code", companyCode);
        return ResponseEntity.ok(ApiResponse.success(shippingOrderService.getShipmentPlanSource(params)));
    }

    @GetMapping("/source/sales-order")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getSalesOrderSource(
            @RequestAttribute("company_code") String companyCode,
            @RequestParam Map<String, Object> params) {
        params.put("company_code", companyCode);
        return ResponseEntity.ok(ApiResponse.success(shippingOrderService.getSalesOrderSource(params)));
    }

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