package com.erp.controller;

import com.erp.dto.ApiResponse;
import com.erp.service.PackagingService;
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/packaging")
@RequiredArgsConstructor
@Slf4j
public class PackagingController {

    private final PackagingService packagingService;

    // ── 포장단위 (pkg_unit) ──────────────────────────────────────────────

    @GetMapping("/pkg-units")
    public ResponseEntity<ApiResponse<List<Map<String, Object>>>> getPkgUnits(
            @RequestAttribute("company_code") String companyCode) {
        Map<String, Object> params = new HashMap<>();
        params.put("company_code", companyCode);
        return ResponseEntity.ok(ApiResponse.success(packagingService.getPkgUnits(params)));
    }

    @PostMapping("/pkg-units")
    public ResponseEntity<ApiResponse<Map<String, Object>>> createPkgUnit(
            @RequestAttribute("company_code") String companyCode,
            @RequestBody Map<String, Object> body) {
        body.put("company_code", companyCode);
        return ResponseEntity.status(201).body(ApiResponse.success(packagingService.createPkgUnit(body)));
    }

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

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

    // ── 포장단위 매칭품목 (pkg_unit_item) ────────────────────────────────

    @GetMapping("/pkg-unit-items/{pkgCode}")
    public ResponseEntity<ApiResponse<List<Map<String, Object>>>> getPkgUnitItems(
            @RequestAttribute("company_code") String companyCode,
            @PathVariable String pkgCode) {
        Map<String, Object> params = new HashMap<>();
        params.put("company_code", companyCode);
        params.put("pkg_code", pkgCode);
        return ResponseEntity.ok(ApiResponse.success(packagingService.getPkgUnitItems(params)));
    }

    @PostMapping("/pkg-unit-items")
    public ResponseEntity<ApiResponse<Map<String, Object>>> createPkgUnitItem(
            @RequestAttribute("company_code") String companyCode,
            @RequestBody Map<String, Object> body) {
        body.put("company_code", companyCode);
        return ResponseEntity.status(201).body(ApiResponse.success(packagingService.createPkgUnitItem(body)));
    }

    @DeleteMapping("/pkg-unit-items/{id}")
    public ResponseEntity<ApiResponse<Map<String, Object>>> deletePkgUnitItem(
            @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(packagingService.deletePkgUnitItem(params)));
    }

    // ── 적재함 (loading_unit) ────────────────────────────────────────────

    @GetMapping("/loading-units")
    public ResponseEntity<ApiResponse<List<Map<String, Object>>>> getLoadingUnits(
            @RequestAttribute("company_code") String companyCode) {
        Map<String, Object> params = new HashMap<>();
        params.put("company_code", companyCode);
        return ResponseEntity.ok(ApiResponse.success(packagingService.getLoadingUnits(params)));
    }

    @PostMapping("/loading-units")
    public ResponseEntity<ApiResponse<Map<String, Object>>> createLoadingUnit(
            @RequestAttribute("company_code") String companyCode,
            @RequestBody Map<String, Object> body) {
        body.put("company_code", companyCode);
        return ResponseEntity.status(201).body(ApiResponse.success(packagingService.createLoadingUnit(body)));
    }

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

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

    // ── 적재함 포장구성 (loading_unit_pkg) ───────────────────────────────

    @GetMapping("/loading-unit-pkgs/{loadingCode}")
    public ResponseEntity<ApiResponse<List<Map<String, Object>>>> getLoadingUnitPkgs(
            @RequestAttribute("company_code") String companyCode,
            @PathVariable String loadingCode) {
        Map<String, Object> params = new HashMap<>();
        params.put("company_code", companyCode);
        params.put("loading_code", loadingCode);
        return ResponseEntity.ok(ApiResponse.success(packagingService.getLoadingUnitPkgs(params)));
    }

    @PostMapping("/loading-unit-pkgs")
    public ResponseEntity<ApiResponse<Map<String, Object>>> createLoadingUnitPkg(
            @RequestAttribute("company_code") String companyCode,
            @RequestBody Map<String, Object> body) {
        body.put("company_code", companyCode);
        return ResponseEntity.status(201).body(ApiResponse.success(packagingService.createLoadingUnitPkg(body)));
    }

    @DeleteMapping("/loading-unit-pkgs/{id}")
    public ResponseEntity<ApiResponse<Map<String, Object>>> deleteLoadingUnitPkg(
            @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(packagingService.deleteLoadingUnitPkg(params)));
    }
}
